ASP.NET 实现文件下载(弹出打开保存文件对话框)
做毕业设计,需要做一个下载模块,大概功能就是弹出一个对话框然后可以自己选择下载路径的功能,希望提供答案的人可以给个详细的答案或者直接发QQ514429551,我会给予加分...
做毕业设计,需要做一个下载模块,大概功能就是弹出一个对话框然后可以自己选择下载路径的功能,希望提供答案的人可以给个详细的答案或者直接发QQ 514429551,我会给予加分的,谢谢
展开
展开全部
public class MyFlush:IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
try
{
//
byte[] bytes = new byte[256];
string filename = Path.GetFileName(context.Request.FilePath);
string path = context.Server.MapPath(filename);
FileStream stream = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read);
//用于文件另存为
context.Response.ContentType = "application/octet-stream";
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
context.Response.AppendHeader("Content-Length",stream.Length.ToString());
//文件总长度
int totalLength = Convert.ToInt32(stream.Length);
if (context.Response.IsClientConnected)
{
while (totalLength > -1)
{
totalLength = totalLength - bytes.Length;
stream.Read(bytes, 0, bytes.Length);
context.Response.OutputStream.Write(bytes,0, bytes.Length);
}
}
stream.Close();
}
catch
{
context.Response.Write(null);
}
}
}
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
try
{
//
byte[] bytes = new byte[256];
string filename = Path.GetFileName(context.Request.FilePath);
string path = context.Server.MapPath(filename);
FileStream stream = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read);
//用于文件另存为
context.Response.ContentType = "application/octet-stream";
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
context.Response.AppendHeader("Content-Length",stream.Length.ToString());
//文件总长度
int totalLength = Convert.ToInt32(stream.Length);
if (context.Response.IsClientConnected)
{
while (totalLength > -1)
{
totalLength = totalLength - bytes.Length;
stream.Read(bytes, 0, bytes.Length);
context.Response.OutputStream.Write(bytes,0, bytes.Length);
}
}
stream.Close();
}
catch
{
context.Response.Write(null);
}
}
}
参考资料: Httphandler
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接连接路径就可以下载了啊 也会弹出对话框的 将路径保存在数据库字段中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询