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(); 

 

posted @ 2012-12-12 14:29  weych  阅读(207)  评论(0编辑  收藏  举报