ASP.Net 下載大文件的實現


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

 

關於此代碼的幾點說明:

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

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

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

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

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

 

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

 

參考文獻: http://support2.microsoft.com/kb/812406


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM