日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NET大文件下載的實(shí)現(xiàn)思路及代碼

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

網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁制作,對成都濕噴機(jī)等多個行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)營銷推廣優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。

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

關(guān)于此代碼的幾點(diǎ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() 來保證當(dāng)前線程可以在最后被終止掉。


本文題目:ASP.NET大文件下載的實(shí)現(xiàn)思路及代碼
URL鏈接:http://www.5511xx.com/article/djocedo.html