国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

ASP.Net 下載大文件的實現(xiàn)

2019-11-17 01:59:27
字體:
供稿:網(wǎng)友

asp.net 下載大文件的實現(xiàn)

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

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

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

2. 根據(jù)下載的文件類型來指定Response.ContentType 。(這個網(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() 來保證當(dāng)前線程可以在最后被終止掉。

 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             try27             {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 type39                 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                     else61                     {62                         // Prevent infinite loop if user disconnects63                         dataToRead = -1;64                     }65                 }66             }67             catch (Exception ex)68             {69                 // Trap the error, if any.70                 Response.Write("Error : " + ex.Message);71             }72             finally73             {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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 清徐县| 水城县| 平潭县| 株洲市| 合川市| 梅州市| 和政县| 宁河县| 江阴市| 建德市| 太仆寺旗| 信宜市| 龙岩市| 桂平市| 炎陵县| 喀什市| 鄯善县| 左贡县| 营山县| 博白县| 兰州市| 漳州市| 巴塘县| 山东| 永嘉县| 庆元县| 三门县| 北安市| 舒城县| 乌鲁木齐市| 澜沧| 华亭县| 错那县| 怀远县| 自治县| 苏尼特左旗| 苗栗县| 昌黎县| 西城区| 灵石县| 西城区|