asp.net实现文件下载

流方式下载

View Code
 1 //string fileName = "file/s.txt"; //定义文件的路径
 2 //string filePath = Server.MapPath(fileName); //将指定的虚拟路径转换成物理路径
 3 ////以字符流的形式下载文件
 4 //FileStream fs = new FileStream(filePath,FileMode.Open);
 5 //byte[] bt = new byte[(int)fs.Length];
 6 //fs.Read(bt,0,bt.Length);
 7 //fs.Close();
 8 //Response.ContentType = "application/octet-stream";
 9 ////通知浏览器下载文件而不是打开文件
10 //Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
11 //Response.BinaryWrite(bt);
12 //Response.Flush();
13 //Response.End(); 

 

 

 writefile下载

View Code
 1 //string fileName = "file/s.txt";
 2 //string filePath = Server.MapPath(fileName);
 3 //FileInfo finfo = new FileInfo(filePath); //提供复制、删除、创建等操作
 4 //Response.Clear();
 5 //Response.ClearContent();
 6 //Response.ClearHeaders();
 7 
 8 //Response.AddHeader("Content-Disposition", "attachment;filename=s.txt");
 9 //Response.AddHeader("Content-Length",finfo.Length.ToString() );
10 //Response.AddHeader("Content-Transfer-Encoding", "binary");
11 //Response.ContentType = "application/octet-stream";
12 //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
13 //Response.WriteFile(finfo.FullName);
14 //Response.Flush();
15 //Response.End(); 

 

转载于:https://www.cnblogs.com/qiangni/archive/2012/12/12/2814585.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值