ASP.NET 大文件下载的实现思路及代码

开发 后端
文件下载是一个网站最基本的功能,ASP.NET网站的文件下载功能实现也很简单,但是如果遇到大文件的下载而不做特殊处理的话,那将会出现不可预料的后果。本文就基于ASP.NET提供大文件下载的实现思路及代码。

[[128441]]

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

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

关于此代码的几点说明:

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

责任编辑:王雪燕 来源: codeceo
相关推荐

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-07-22 17:35:23

代码隐藏文件ASP.NET

2009-08-10 17:17:10

ASP.NET安装部署

2009-07-31 11:45:42

ASP.NET文件下载

2009-07-24 10:41:00

ASP.NET Ses

2023-09-06 08:33:30

2009-08-05 16:59:55

ASP.NET组件设计

2009-07-31 13:24:43

ASP.NET AJA

2009-08-04 14:18:49

ASP.NET邮件列表

2009-08-12 14:10:37

asp.net分页代码

2009-08-04 11:29:14

HTML代码ASP.NET控件

2009-08-04 14:36:00

ASP.NET分页管理

2009-08-14 13:37:25

ASP.NET静态页面

2009-07-20 15:44:32

ASP.NET MVC

2009-08-04 17:16:16

ASP.NET代码优化

2009-07-24 10:23:07

WORD文件转换PDFASP.NET
点赞
收藏

51CTO技术栈公众号