asp.net 文件下载(txt,rar,pdf,word,excel,ppt)

    aspx 文件下载说起来一点都不难,但是在做的过程中还是遇到了一些小小的问题,就是因为这些小小的问题,导致解决起来实在是太难了,其中一个就是Response.End();导致下载文件出现线程终止的情况...

正确的下载文件的方法

 1              //获取对应文件的内容,这里主要取comm.FileURL的文件保存动态路径,也就是20150825/5e7af276b7754363a1e78b496e1d1603文本文档.txt
 2             CommNoticeModel comm = CommNoticeBLL.GetInstance().GetCommNoticeModel(int.Parse(Request.QueryString["ID"]));
 3             //这里主要组成文件的相对路径,这里得到的就是  ~/FileBox/20150825/5e7af276b7754363a1e78b496e1d1603文本文档.txt
 4             string path = CommonUtilModel.GetFileVirtualPath() + comm.FileURL;
 5             try
 6             {
 7                               
 8                 FileInfo fileInfo = new FileInfo(Server.MapPath(path));
 9                 Response.Clear();
10                 Response.ClearContent();
11                 Response.ClearHeaders();
12                 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
13                 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
14                 Response.AddHeader("Content-Transfer-Encoding", "binary");
15                 // 告诉浏览器传递给用用户的是一个非txt,rar等不出现在IEEM上的一个文件,不需要在浏览器页面打开,需要直接下载
16                 Response.ContentType = "application/octet-stream";
17                 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
18                 Response.WriteFile(fileInfo.FullName);
19                 Response.Flush();
20                 Response.End();             
21             }
22 
23             catch (Exception ex)
24             {
25                 Response.Clear();
26                 Response.ClearContent();
27                 Response.ClearHeaders();
28                 Response.Write(ex.Message + "<br/>" + path);
29                 Response.End();
30             }

转载请注明出处:http://www.cnblogs.com/abc1069/

转载于:https://www.cnblogs.com/abc1069/p/4756739.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值