ASP.NET中下載檔案
ASP.NET中下載檔案
//TransmitFile實現下載 protected void Button1_Click(object sender, EventArgs e) { /* 微軟為Response物件提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的檔案時導致Aspnet_wp.exe程式回收而無法成功下載的問題。 程式碼如下: */ 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) { /* using System.IO; */ 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(); } //---------------------------------------------------------- public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody ) { WebForm.Response.ClearHeaders(); WebForm.Response.Clear(); WebForm.Response.Expires = 0; WebForm.Response.Buffer = true; WebForm.Response.AddHeader("Accept-Language", "zh-tw"); //'檔名稱 WebForm.Response.AddHeader("content-disposition", "attachment; filename='"+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+"'"); WebForm.Response.ContentType = "Application/octet-stream"; //'檔案內容 WebForm.Response.Write(FileBody);//----------- WebForm.Response.End(); } //上面這段程式碼是下載一個動態產生的文字檔案,若這個檔案已經存在於伺服器端的實體路徑,則可以透過下面的函式: public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath ) { WebForm.Response.ClearHeaders(); WebForm.Response.Clear(); WebForm.Response.Expires = 0; WebForm.Response.Buffer = true; WebForm.Response.AddHeader("Accept-Language", "zh-tw"); //檔名稱 WebForm.Response.AddHeader("content-disposition", "attachment; filename='" + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) +"'" ); WebForm.Response.ContentType = "Application/octet-stream"; //檔案內容 WebForm.Response.Write(System.IO.File.ReadAllBytes(FilePath));//--------- WebForm.Response.End(); } |
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13923422/viewspace-1007493/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 如何在ASP.NET中下載檔案ASP.NET
- 在 RESTful Web 服務中下載和上傳檔案 - mscharhagRESTWeb
- ASP.NET 檔案下載ASP.NET
- JSP網頁中下載xls檔案亂碼怎麼辦?JS網頁
- asp.net 檔案下載與壓縮ASP.NET
- 請問resin中下載的word檔案內容為亂碼該如何解決?
- ASP.NET檔案下載的實用方法ASP.NET
- android從IIS/asp.net下載apk檔案AndroidASP.NETAPK
- ASP.NET中檔案上傳下載方法集合ASP.NET
- ASP.NET利用HttpHandler實現多副檔名檔案下載ASP.NETHTTP
- Asp.net C# 檔案下載,附件下載程式碼案例,不顯示檔案路徑ASP.NETC#
- 【ASP.NET Core】Blazor+MiniAPI完成檔案下載ASP.NETBlazorAPI
- asp.net 檔案上傳和下載管理原始碼ASP.NET原始碼
- ASP.NET下載檔案(彈出開啟儲存檔案對話方塊)ASP.NET
- Asp.net直接儲存(下載)檔案到客戶端ASP.NET客戶端
- ASP.NET工程檔案(.csproj)檔案解讀ASP.NET
- windows在anaconda中下載安裝fasttextWindowsAST
- ASP.NET 大檔案下載的實現思路及程式碼ASP.NET
- 淺談ASP.NET中檔案下載函式使用方法ASP.NET函式
- asp.net response.ContentType 下載檔案的四種方法ASP.NET
- ASP.NET Core 配置檔案ASP.NET
- ASP.NET Core - 入口檔案ASP.NET
- NiceTool工具解決微信中下載APPAPP
- IDEA如何找到在IDEA中下載jdkIdeaJDK
- 檔案流下載檔案,zip/其他格式檔案
- vue中下載excel的使用,後端連結兩種情況,一個是連結,一個是檔案流VueExcel後端
- 【Azure Developer - 金鑰保管庫 】使用 Python Azure SDK 實現從 Azure Key Vault Certificate 中下載證照(PEM檔案)DeveloperPython
- asp.net 中兩種不同方式檔案流下載的區別?ASP.NET
- ASP.NET Core 檔案上傳ASP.NET
- 檔案下載
- 簡單介紹ASP.NET Core實現檔案上傳和下載ASP.NET
- 檔案程式設計、檔案下載程式設計
- ASP.NET MVC 匯入Excel檔案ASP.NETMVCExcel
- asp.net (C#)生成html檔案ASP.NETC#HTML
- asp.net 操作INI配置檔案類ASP.NET
- 使用ASP.NET Global.asax 檔案ASP.NET
- 用ASP.NET上傳大檔案ASP.NET
- SpringMVC檔案上傳下載(單檔案、多檔案)SpringMVC