asp.net裡,各種下載方式匯總

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

 1         /// <summary> 2         /// 下載檔案 TransmitFile 3         /// </summary> 4         /// <param name="filePath"></param> 5         public static void DownloadFile(string filePath) 6         { 7             string filename = HttpContext.Current.Server.MapPath(filePath); 8             var response = HttpContext.Current.Response; 9             response.ContentType = "application/x-zip-compressed";10             response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(filename) );11             response.TransmitFile(filename); 12         }13 14         /// <summary>15         /// 下載大檔案16         /// </summary>17         /// <param name="filePath"></param>18         public static void DownloadBigFile(string filePath)19         {20             filePath =  HttpContext.Current.Server.MapPath(filePath);//路徑21             string fileName = Path.GetFileName(filePath);//用戶端儲存的檔案名稱22             var response = HttpContext.Current.Response;23             System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);24             if (fileInfo.Exists == true)25             {26                 const long ChunkSize = 102400;//100K 每次讀取檔案,唯讀取100K,這樣可以緩解伺服器的壓力27 28                 byte[] buffer = new byte[ChunkSize];29 30                 response.Clear();31                 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);32                 long dataLengthToRead = iStream.Length;//擷取下載的檔案總大小33                 response.ContentType = "application/octet-stream";34                 response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));35                 while (dataLengthToRead > 0 && response.IsClientConnected)36                 {37                     //讀取的大小 Response.OutputStream.Write(buffer, 0, lengthRead);38                     int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));39                     response.Flush();40                     dataLengthToRead = dataLengthToRead - lengthRead;41                 }42 43                 response.Close();44             }45         }46 47         /// <summary>48         /// 流方式下載49         /// </summary>50         /// <param name="filePath"></param>51         public static void DownloadFileStream(string filePath)52         {53             filePath = HttpContext.Current.Server.MapPath(filePath);//路徑54             string fileName = Path.GetFileName(filePath);//用戶端儲存的檔案名稱55             var response = HttpContext.Current.Response;56             FileStream fs = new FileStream(filePath, FileMode.Open);57 58             byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length);59 60             fs.Close();61             response.ContentType = "application/octet-stream";62             //通知瀏覽器下載檔案而不是開啟63             response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));64             response.BinaryWrite(bytes);65             response.Flush();66             response.End();67         }68 69         /// <summary>70         /// 將文本作為某檔案下載71         /// </summary>72         /// <param name="text"></param>73         /// <param name="fileName"></param>74         public static void DownloadText(string text , string fileName)75         {76             byte[] bytes = Encoding.Default.GetBytes(text);77             var response = HttpContext.Current.Response;78             response.ContentType = "application/octet-stream";79             //通知瀏覽器下載檔案而不是開啟80             response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));81             response.BinaryWrite(bytes);82             response.Flush();83             response.End();84         }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.