C# Net MVC 大檔案下載幾種方式、支援速度限制、資源佔用小
上一篇我們說到大檔案的分片下載、斷點續傳、秒傳,有的博友就想看分片下載,我們也來總結一下下載的幾種方式,寫的比較片面,大家見諒^_^。
下載方式:
1、html超連結下載;
2、後臺下載(四種方法:返回filestream、返回file、TransmitTile方法、Response分塊下載)。
1、html超連結下載
超級連結在本質上屬於一個網頁的一部分,它是一種允許我們同其他網頁或站點之間進行連線的元素。
各個網頁連結在一起後,才能真正構成一個網站。
所謂的超連結是指從一個網頁指向一個目標的連線關係,這個目標可以是另一個網頁,也可以是相同網頁
上的不同位置,還可以是一個圖片,一個電子郵件地址,一個檔案,甚至是一個應用程式。
超連結的種類(一般有四種:http,file,ftp,maito):
1. http 如:<a href="http://www.baidu.com">百度</a>
2. file 如:<a href="file://images/1.jpg">圖片</a>
3. ftp 如:<a href="ftp://192.168.0.0/">進入</a>
4. mailto 如:<a href="mailto:[email protected]">e-mail</a>
2、後臺下載(四種方法:返回filestream、返回file、TransmitTile方法、Response分塊下載)
前臺請求後臺,後臺做出響應進行資料下載。至於請求方式可以多樣,比如:a標籤跳轉,ajax請求等均可。
我們來看後臺響應的四種方式:
1、返回filestream
/// <summary> /// 返回filestream /// </summary> /// <returns></returns> public FileStreamResult filestream_download() { string fileName = "wenjian.txt";//客戶端儲存的檔名 string filePath = Server.MapPath("/Upload/wenjian.txt");//指定檔案所在的全路徑 return File(new FileStream(filePath, FileMode.Open), "text/plain",//"text/plain"是檔案MIME型別 fileName); }
2、返回file
/// <summary> /// 返回file /// </summary> /// <returns></returns> public FileResult file_download() { string filePath = Server.MapPath("/Upload/wenjian.txt");//路徑 return File(filePath, "text/plain", "wenjian.txt"); //"text/plain"是檔案MIME型別,welcome.txt是客戶端儲存的名字 }
3、TransmitFile方法
/// <summary> /// TransmitFile方法 /// </summary> public bool TransmitFile_download() { string fileName = "wenjian.txt";//客戶端儲存的檔名 string filePath = Server.MapPath("/Upload/wenjian.txt");//路徑 FileInfo fileinfo = new FileInfo(filePath); Response.Clear();//清除緩衝區流中的所有內容輸出 Response.ClearContent();//清除緩衝區流中的所有內容輸出 Response.ClearHeaders();//清除緩衝區流中的所有頭 Response.Buffer = true;//該值指示是否緩衝輸出,並在完成處理整個響應之後將其傳送 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length",fileinfo.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "binary"); Response.ContentType = "application/unknow";//獲取或設定輸出流的 HTTP MIME 型別 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); //獲取或設定輸出流的 HTTP 字符集 Response.TransmitFile(filePath); Response.End(); return true; }
4、Response分塊下載
/// <summary> ///Response分塊下載,輸出硬碟檔案,提供下載 支援大檔案、續傳、速度限制、資源佔用小 /// </summary> /// <param name="fileName">客戶端儲存的檔名</param> /// <param name="filePath">客戶端儲存的檔案路徑(包括檔名)</param> /// <returns></returns> public bool ResponseDownLoad(string fileName, string filePath) { fileName = "wenjian.txt";//客戶端儲存的檔名 filePath = Server.MapPath("/Upload/wenjian.txt"); //路徑(後續從webconfig讀取) 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(); return true; } else return false; }
總結:
以上就是我所瞭解的幾種下載方式,個人比較中意Response分塊下載。
感謝大家的支援,寫的不好望大家見諒。^_^