成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

[[128441]]

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

  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)于此代碼的幾點(diǎn)說(shuō)明:

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

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

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

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

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

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

責(zé)任編輯:王雪燕 來(lái)源: 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-08-10 17:17:10

ASP.NET安裝部署

2009-07-24 10:41:00

ASP.NET Ses

2009-07-22 17:35:23

代碼隱藏文件ASP.NET

2009-07-31 11:45:42

ASP.NET文件下載

2024-05-20 13:06:18

2023-09-06 08:33:30

2009-08-05 16:59:55

ASP.NET組件設(shè)計(jì)

2009-08-04 14:18:49

ASP.NET郵件列表

2009-07-31 13:24:43

ASP.NET AJA

2009-08-04 11:29:14

HTML代碼ASP.NET控件

2009-08-12 14:10:37

asp.net分頁(yè)代碼

2024-12-05 08:14:41

2009-08-14 13:37:25

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

2009-08-04 14:36:00

ASP.NET分頁(yè)管理

2009-07-20 15:44:32

ASP.NET MVC
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 日本免费一区二区三区 | 亚洲一区二区三区免费观看 | 天天躁日日躁狠狠的躁天龙影院 | 久久99精品久久久 | 日本手机看片 | 国产乱码精品1区2区3区 | 精品欧美一区二区三区免费观看 | 日韩高清一区 | 妹子干综合 | 99亚洲精品 | 久久精品中文字幕 | 美女久久视频 | 亚洲成人自拍网 | 成人亚洲片 | 欧美性生活视频 | 99成人免费视频 | 孰女乱色一区二区三区 | 91看片官网 | 午夜影院 | 成人h动漫精品一区二区器材 | 亚洲欧美激情精品一区二区 | 精品国产乱码久久久久久闺蜜 | 91九色视频在线 | 成人网在线观看 | 国产精品久久精品 | 亚洲精品乱码久久久久久蜜桃91 | 精品欧美视频 | 国产69久久精品成人看动漫 | a国产一区二区免费入口 | 国产伦一区二区三区四区 | 中文字幕av一区二区三区 | 成人午夜免费福利视频 | 日日做夜夜爽毛片麻豆 | 色中文在线 | 精精国产xxxx视频在线播放7 | 亚洲综合久久精品 | 久久精品综合网 | 日韩无| 国产欧美在线一区 | 四虎成人免费视频 | 国产精品高潮呻吟久久av野狼 |