Response.WriteFile 无法下载大文件

系统 1673 0

原因

loadTOCNode(1, 'cause');
Web 服务器计算机的硬件配置决定您可以成功下载的最大文件大小。当 ASP.NET 辅助进程(Aspnet_wp.exe,对于在 Internet 信息服务 6.0 [IIS] 上运行的应用程序,则为 W3wp.exe)执行文件下载请求时,会出现文件下载对话框。ASP.NET 辅助进程开始向 Microsoft Internet 信息服务进程(Inetinfo.exe 或 Dllhost.exe)发送数据。它不等您单击“确定”即开始发送。

根据计算机的配置,IIS 进程可能会处理数据,也可能会将数据缓存在内存中。如果文件太大,在这两个进程相互通信的过程中,数据将被缓存在内存中。这可能会导致服务器上的内存使用量增加。出现此错误的原因是 Web 服务器上的内存限制。

回到顶端

替代方法

loadTOCNode(1, 'workaround');
要解决此问题,请使用以下任一方法:
将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。以下代码演示了如何完成此操作。

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

此外,在下面的代码中,您还必须知道与 FileStream 构造函数一起使用的参数值。指定的枚举值会对提供的功能产生重大影响。有关更多信息,请参考 参考 一节中的 FileStream 链接。
Visual C# .NET 代码
      	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 = "DownloadFileName";

// 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.ContentType = "application/octet-stream";
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 disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
      - 或 -
    


为用户提供用于下载文件的链接。

- 或 -
使用 Microsoft ASP 3.0 进行下载或者与 ASP 一起使用 Software Artisans FileUp。

- 或 -
创建 ISAPI 扩展以下载文件。

- 或 -
使用 FTP 下载文件。
DownloadFileName 替换为大于 100 MB 的文件的名称。


<!-- Search Google -->
输入您的搜索字词 提交搜索表单
<!-- google_ad_client = "pub-7330597899926046"; google_ad_format = "350x30_sdo"; google_link_target = 2; google_color_bg = "ffffff"; google_color_link = "000000"; google_encoding = "GB2312"; //-->
<!-- Search Google --> <!-- google_ad_client = "pub-7330597899926046"; google_ad_slot = "8791774696"; google_ad_width = 468; google_ad_height = 60; //-->

Response.WriteFile 无法下载大文件


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论