https://support.microsoft.com/zh-cn/kb/812406

http://www.cnblogs.com/luisliu/p/4253815.html

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

关于此代码的几点说明:

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

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

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

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

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

using 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[]; // 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 = "application/octet-stream"; // Set the file type
Response.AddHeader("Content-Length", dataToRead.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); // Read the bytes.
while (dataToRead > )
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, , ); // Write the data to the current output stream.
Response.OutputStream.Write(buffer, , length); // Flush the data to the HTML output.
Response.Flush(); buffer = new Byte[];
dataToRead = dataToRead - length;
}
else
{
// Prevent infinite loop if user disconnects
dataToRead = -;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
} Response.End();
}
}
}
}

重要说明:当您在 ASP.NET 应用程序的 Web.config 文件中将编译元素的 debug 属性值设置为 false 时,必须针对要下载的文件大小将Server.ScriptTimeout 属性设置为适当的值。默认情况下,Server.ScriptTimeout 值被设置为 90 秒。但是,当 debug 属性被设置为 true时,Server.ScriptTimeout 值将被设置为一个非常大的值(30,000,000 秒)。作为一名开发人员,您必须知道这可能会对您的 ASP.NET Web 应用程序的行为造成的影响。

VB。NET代码如下:

Dim iStream As System.IO.Stream

      ' Buffer to read 10K bytes in chunk:
Dim buffer() As Byte ' Length of the file:
Dim length As Integer ' Total bytes to read:
Dim dataToRead As Long ' Identify the file to download including its path.
Dim filepath As String = "DownloadFileName" ' Identify the file name.
Dim filename As String = System.IO.Path.GetFileName(filepath) Try
' Open the file.
iStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, _
IO.FileAccess.Read, IO.FileShare.Read) ' Total bytes to read:
dataToRead = iStream.Length Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=" & filename) ' Read the bytes.
While dataToRead >
' Verify that the client is connected.
If Response.IsClientConnected Then
' Read the data in buffer
length = iStream.Read(buffer, , ) ' Write the data to the current output stream.
Response.OutputStream.Write(buffer, , length) ' Flush the data to the HTML output.
Response.Flush() ReDim buffer() ' Clear the buffer
dataToRead = dataToRead - length
Else
'prevent infinite loop if user disconnects
dataToRead = -
End If
End While Catch ex As Exception
' Trap the error, if any.
Response.Write("Error : " & ex.Message)
Finally
If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
End If
End Try

最新文章

  1. 自定义getElementByClass
  2. redis的主从复制配置
  3. js 字符串分割成字符串数组 遍历数组插入指定DOM里 原生JS效果
  4. 配置editplus,讓其支持代碼自動格式化功能.
  5. jQuery.prop() 使用详解
  6. linux进程的几种状态
  7. thinkjs—控制器方法名不能大写
  8. setData优化过程
  9. 用SVG做background image
  10. 爬虫之进阶 twisted
  11. 在Postman中使用不受信任的SSL证书(转)
  12. 20175314 《Java程序设计》迭代和JDB
  13. andorid 计算器
  14. SQLServer 常见SQL笔试题之语句操作题详解
  15. Mac或者linux下登陆到linux上的SFTP
  16. iOS边练边学--(Quartz2D)基本图形的绘制#附加自定义进度控件的练习
  17. centos7删除原docker 安装新docker-ce
  18. Node.js 搭建Web
  19. iPhone开发资源汇总
  20. 监控系统cpu相关统计信息

热门文章

  1. ubuntu 常用命令
  2. 通俗理解隐马尔科夫模型HMM(转载)
  3. UIApplication的使用
  4. 盯盯拍Android App 3.0指导
  5. 使用swipecard实现卡片视图左右滑动监听以及点击监听
  6. JQuery面试题答案
  7. 高度平衡的二叉搜索树(AVL树)
  8. twig一些常用的用法总结【原创】
  9. android学习笔记53——自动朗读TTS
  10. jQuery入门级part.2