ASP.NET 大文件下载的实现思路及代码
创始人
2025-01-10 20:41:15
0次

[[128441]]

当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。

  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. } 

关于此代码的几点说明:

1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。

2. 根据下载的文件类型来指定 Response.ContentType 。(参考OSChina的这个网址可以找到大部分文件类型的对照表:http://tool.oschina.net/commons)

3. 在每次写完response时记得调用 Response.Flush()

4. 在循环下载的过程中使用 Response.IsClientConnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。

5. 在下载结束后,需要调用 Response.End() 来保证当前线程可以在最后被终止掉。

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

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
粉嫩如何诠释霸道 东芝M805... “霸道粉”是个什么玩意东芝M805拿过来的时候,笔者扑哧笑了,不是笑这款笔记本,而是笑这款产品的颜色...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
只需三步,让你掌握阿尔法路由器... 请参照以下阿尔法路由器设置方法,通过浏览器进入及设置宽带路由器。经过这三个步骤的设置,基本上能够熟悉...
如何更省钱? 近期特价热门硒鼓... 如今激光打印设备已经成为商务或企业的绝对主力办公设备,但采购者往往忽视了激光打印设备所用的耗材,相对...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...