自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET 大文件下載的實現(xiàn)思路及代碼

開發(fā) 后端
文件下載是一個網(wǎng)站最基本的功能,ASP.NET網(wǎng)站的文件下載功能實現(xiàn)也很簡單,但是如果遇到大文件的下載而不做特殊處理的話,那將會出現(xiàn)不可預料的后果。本文就基于ASP.NET提供大文件下載的實現(xiàn)思路及代碼。

[[128441]]

當我們的網(wǎng)站需要支持下載大文件時,如果不做控制可能會導致用戶在訪問下載頁面時發(fā)生無響應(yīng),使得瀏覽器崩潰。可以參考如下代碼來避免這個問題。

  1. using System; 
  2. namespace WebApplication1 
  3.     public partial class DownloadFile : System.Web.UI.Page 
  4.     { 
  5.         protected void Page_Load(object sender, EventArgs e) 
  6.         { 
  7.             System.IO.Stream iStream = null
  8.             // Buffer to read 10K bytes in chunk: 
  9.             byte[] buffer = new Byte[10000]; 
  10.             // Length of the file: 
  11.             int length; 
  12.             // Total bytes to read. 
  13.             long dataToRead; 
  14.             // Identify the file to download including its path. 
  15.             string filepath = Server.MapPath("/") +"./Files/TextFile1.txt"
  16.             // Identify the file name. 
  17.             string filename = System.IO.Path.GetFileName(filepath); 
  18.             try 
  19.             { 
  20.                 // Open the file. 
  21.                 iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, 
  22.                             System.IO.FileAccess.Read, System.IO.FileShare.Read); 
  23.                 // Total bytes to read. 
  24.                 dataToRead = iStream.Length; 
  25.                 Response.Clear(); 
  26.                 Response.ClearHeaders(); 
  27.                 Response.ClearContent(); 
  28.                 Response.ContentType = "text/plain"// Set the file type 
  29.                 Response.AddHeader("Content-Length", dataToRead.ToString()); 
  30.                 Response.AddHeader("Content-Disposition""attachment; filename=" + filename); 
  31.                 // Read the bytes. 
  32.                 while (dataToRead > 0
  33.                 { 
  34.                     // Verify that the client is connected. 
  35.                     if (Response.IsClientConnected) 
  36.                     { 
  37.                         // Read the data in buffer. 
  38.                         length = iStream.Read(buffer, 010000); 
  39.                         // Write the data to the current output stream. 
  40.                         Response.OutputStream.Write(buffer, 0, length); 
  41.                         // Flush the data to the HTML output. 
  42.                         Response.Flush(); 
  43.                         buffer = new Byte[10000]; 
  44.                         dataToRead = dataToRead - length; 
  45.                     } 
  46.                     else 
  47.                     { 
  48.                         // Prevent infinite loop if user disconnects 
  49.                         dataToRead = -1
  50.                     } 
  51.                 } 
  52.             } 
  53.             catch (Exception ex) 
  54.             { 
  55.                 // Trap the error, if any. 
  56.                 Response.Write("Error : " + ex.Message); 
  57.             } 
  58.             finally 
  59.             { 
  60.                 if (iStream != null
  61.                 { 
  62.                     //Close the file. 
  63.                     iStream.Close(); 
  64.                 } 
  65.                 Response.End(); 
  66.             } 
  67.         } 
  68.     } 

關(guān)于此代碼的幾點說明:

1. 將數(shù)據(jù)分成較小的部分,然后將其移動到輸出流以供下載,從而獲取這些數(shù)據(jù)。

2. 根據(jù)下載的文件類型來指定 Response.ContentType 。(參考OSChina的這個網(wǎng)址可以找到大部分文件類型的對照表:http://tool.oschina.net/commons)

3. 在每次寫完response時記得調(diào)用 Response.Flush()

4. 在循環(huán)下載的過程中使用 Response.IsClientConnected 這個判斷可以幫助程序盡早發(fā)現(xiàn)連接是否正常。若不正常,可以及早的放棄下載,以釋放所占用的服務(wù)器資源。

5. 在下載結(jié)束后,需要調(diào)用 Response.End() 來保證當前線程可以在最后被終止掉。

原文鏈接:http://www.codeceo.com/article/asp-net-big-file.html

責任編輯:王雪燕 來源: codeceo
相關(guān)推薦

2009-07-21 15:38:31

2009-07-20 16:09:39

2010-02-05 08:32:32

ASP.NET MVC

2009-07-21 16:05:58

ASP.NET大文件上

2009-07-22 17:13:21

Asp.Net編程

2009-07-22 17:35:23

代碼隱藏文件ASP.NET

2009-08-10 17:17:10

ASP.NET安裝部署

2009-07-24 10:41:00

ASP.NET Ses

2024-05-20 13:06:18

2009-07-31 11:45:42

ASP.NET文件下載

2009-08-05 16:59:55

ASP.NET組件設(shè)計

2023-09-06 08:33:30

2009-07-31 13:24:43

ASP.NET AJA

2009-08-04 14:18:49

ASP.NET郵件列表

2009-08-12 14:10:37

asp.net分頁代碼

2009-08-04 11:29:14

HTML代碼ASP.NET控件

2024-12-05 08:14:41

2009-08-14 13:37:25

ASP.NET靜態(tài)頁面

2009-08-04 14:36:00

ASP.NET分頁管理

2009-08-04 17:16:16

ASP.NET代碼優(yōu)化
點贊
收藏

51CTO技術(shù)棧公眾號