C#下载4种方法(转载: http://topic.csdn.net/u/20120822/10/d8115bb7-2f2a-4c2f-b0c1-aab68bcb1e3e.html)
2012-08-30 10:20
477 查看
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=z.zip"); string filename = Server.MapPath("DownLoad/z.zip"); Response.TransmitFile(filename); } //WriteFile实现下载 protected void Button2_Click(object sender, EventArgs e) { string fileName = "asd.txt";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 FileInfo fileInfo = new FileInfo(filePath); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length", fileInfo.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "binary"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(fileInfo.FullName); Response.Flush(); Response.End(); } //WriteFile分块下载 protected void Button3_Click(object sender, EventArgs e) { string fileName = "aaa.txt";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true) { const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 byte[] buffer = new byte[ChunkSize]; Response.Clear(); System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); long dataLengthToRead = iStream.Length;//获取下载的文件总大小 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); while (dataLengthToRead > 0 && Response.IsClientConnected) { int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 Response.OutputStream.Write(buffer, 0, lengthRead); Response.Flush(); dataLengthToRead = dataLengthToRead - lengthRead; } Response.Close(); } } //流方式下载 protected void Button4_Click(object sender, EventArgs e) { string fileName = "aaa.txt";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } }
相关文章推荐
- SQL优化34条(转载至http://topic.csdn.net/u/20080808/14/dfc973e7-fcf2-4526-9de1-fa5efc148f5c.html?seed=1579461806)
- js 调用C#.NET后台方法 转载自:http://www.cnblogs.com/lizhao/archive/2010/11/23/1990436.html
- 优秀网站源码、编程源码下载网站大集中(http://topic.csdn.net/u/20080501/12/45ab4636-6566-4ffe-8ec0-b99da747ea5b.html)
- cxGrid使用技巧 转载自http://topic.csdn.net/t/20060106/09/4500997.html#
- cxGrid使用技巧 转载自http://topic.csdn.net/t/20060106/09/4500997.html#
- 如何用C#编程方式批量对域控制器添加OU(http://topic.csdn.net/t/20051026/11/4351176.html)
- 动态sql引用(原贴地址http://topic.csdn.net/u/20090313/10/30cd0407-3d5b-4fee-bb79-3897c137cba6.html)
- delphi 拖放操作http://topic.csdn.net/t/20030306/10/1497344.html?1026446129
- 当窗口大小变化时,始终让底图居中--转载http://topic.csdn.net/t/20060824/16/4973302.html
- 转帖 一个很有用但不常用到的传值方法! http://topic.csdn.net/u/20070124/22/09d43606-4119-4407-bfa6-8b2890ffbac7.html
- 动态sql引用(原贴地址http://topic.csdn.net/u/20090313/10/30cd0407-3d5b-4fee-bb79-3897c137cba6.html)
- Eclipse快捷键【http://topic.csdn.net/u/20100806/10/b6f5c08e-5bba-4361-a761-c89b20d42824.html?47565】
- http://topic.csdn.net/u/20090612/20/258ea941-c80b-47ad-b314-285b82281050.html
- 在asp.net中读取XML文件信息的4种方法 (http://www.cnblogs.com/liping19851014/archive/2007/10/16/926137.html)
- ROSE 好的参考资料 http://topic.csdn.net/t/20020716/14/878323.html
- 改变表的所有者http://topic.csdn.net/t/20020221/19/536354.html
- oracle 的数据恢复 转载(http://topic.csdn.net/u/20100819/16/0ba394dc-c81e-44d2-9da7-710f0ab7c7b4.html?57387)
- Java源代码的折行规则(转载自http://news.csdn.net/a/20090929/213943.html)
- http://topic.csdn.net/u/20080627/10/26171a67-bcf2-4463-a93f-b0cbe622ce78.html
- 调侃的语言谈一谈编写代码(转载CSDN)http://sd.csdn.net/a/20110603/299247.html