C# 檔案下載四方法
C# 檔案下載四方法
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(); } }
相關文章
- C#檔案下載C#
- C# 檔案下載 : WinINetC#
- asp.net response.ContentType 下載檔案的四種方法ASP.NET
- 下載檔案的15種方法
- 15種下載檔案的方法
- C# 檔案下載之斷點續傳C#斷點
- Asp.net C# 檔案下載,附件下載程式碼案例,不顯示檔案路徑ASP.NETC#
- Thinkphp3.2 下載檔案的方法PHP
- 檔案下載
- 下載GitHub上檔案的兩種方法Github
- 【liunx命令】上傳下載檔案的方法
- python下載檔案的三種方法Python
- jsp下載檔案的實現方法JS
- 使用 C# 下載檔案的十八般武藝C#
- 教你如何實現c#檔案上傳下載功能C#
- java實現檔案的下載的方法概述Java
- iis支援.apk檔案下載的設定方法APK
- ASP.NET檔案下載的實用方法ASP.NET
- 00、下載檔案
- Ajax 下載檔案
- FastApi下載檔案ASTAPI
- MVC 下載檔案MVC
- js 檔案下載JS
- Servlet下載檔案Servlet
- httpWebRequest 檔案下載HTTPWeb
- php檔案下載PHP
- .net 檔案下載
- php 檔案下載PHP
- HttpClient 下載檔案HTTPclient
- Response下載檔案
- 檔案程式設計、檔案下載程式設計
- iOS開發網路篇之檔案下載、大檔案下載、斷點下載iOS斷點
- windows下ftp定時執行批次下載檔案,windows下ftp定時執行批次下載檔案的一種方法WindowsFTP
- ASP.NET中檔案上傳下載方法集合ASP.NET
- Flutter 下載檔案操作Flutter
- SpringMVC檔案下載SpringMVC
- 【springmvc】下載檔案SpringMVC
- 使用PHP下載檔案PHP