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

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

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

2019-11-17 02:01:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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

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

  • <span id="ekaw7"></span>
  • <center id="ekaw7"><dl id="ekaw7"><dfn id="ekaw7"></dfn></dl></center>
      • 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970using System;namespace Webapplication1{public partial class DownloadFile : System.Web.UI.Page{PRotected void Page_Load(object sender, EventArgs e){System.IO.Stream iStream = null;// Buffer to read 10K bytes in chunk:byte[] buffer = new Byte[10000];// Length of the file:int length;// Total bytes to read.long dataToRead;// Identify the file to download including its path.string filepath = Server.MapPath("/") +"./Files/TextFile1.txt";// Identify the file name.string filename = System.IO.Path.GetFileName(filepath);try{// Open the file.iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,System.IO.Fileaccess.Read, System.IO.FileShare.Read);// Total bytes to read.dataToRead = iStream.Length;Response.Clear();Response.ClearHeaders();Response.ClearContent();Response.ContentType = "text/plain"; // Set the file typeResponse.AddHeader("Content-Length", dataToRead.ToString());Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);// Read the bytes.while (dataToRead > 0){// Verify that the client is connected.if (Response.IsClientConnected){// Read the data in buffer.length = iStream.Read(buffer, 0, 10000);// Write the data to the current output stream.Response.OutputStream.Write(buffer, 0, length);// Flush the data to the HTML output.Response.Flush();buffer = new Byte[10000];dataToRead = dataToRead - length;}else{// Prevent infinite loop if user disconnectsdataToRead = -1;}}}catch (Exception ex){// Trap the error, if any.Response.Write("Error : " + ex.Message);}finally{if (iStream != null){//Close the file.iStream.Close();}
        發(fā)表評(píng)論 共有條評(píng)論
        用戶名: 密碼:
        驗(yàn)證碼: 匿名發(fā)表
        主站蜘蛛池模板: 隆林| 水富县| 苍溪县| 卢龙县| 凤城市| 保山市| 白水县| 东丰县| 安乡县| 岚皋县| 红安县| 湛江市| 清远市| 淮滨县| 淅川县| 厦门市| 新巴尔虎左旗| 濉溪县| 博兴县| 太和县| 育儿| 马山县| 理塘县| 凉城县| 周至县| 甘南县| 徐闻县| 滕州市| 治县。| 巴南区| 新建县| 蛟河市| 盱眙县| 镶黄旗| 房产| 准格尔旗| 封开县| 澎湖县| 巴彦县| 濮阳县| 宁化县|
        <pre id="ekaw7"><div id="ekaw7"><i id="ekaw7"></i></div></pre>

        <small id="ekaw7"></small>
        <small id="ekaw7"></small>
        • <small id="ekaw7"></small>