gpt4 book ai didi

返回大于 1 GB 的大文件时,算术运算中的 ASP.NET 溢出或下溢

转载 作者:行者123 更新时间:2023-12-03 20:18:16 25 4
gpt4 key购买 nike

我在 ASP.NET 中遇到了某种限制。
我将问题简化为 ASP.NET MVC 项目(使用 Visual Studio 2010 和 .NET 4 创建)中的示例项目,但问题仍然存在:

在 MVC Controller 中,我有一个提供文件下载的方法:

public ActionResult DownloadBigFile()
{
string file = @"C:\Temp\File.txt";
var readStream = new FileStream(file, FileMode.Open, FileAccess.Read);
return File(readStream, "text/plain", "FILE");
}

当文件低于 1 GB 时,下载工作正常,
超过 1 GB 时会引发异常:“算术运算中的溢出或下溢”,详细信息如下:
 Overflow or underflow in the arithmetic operation.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArithmeticException: Overflow or underflow in the arithmetic operation.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[ArithmeticException: Overflow or underflow in the arithmetic operation.]

[HttpException (0x80004005): An error occurred while communicating with the remote host. The error code is 0x80070216.]
System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect) +4081269
System.Web.Hosting.IIS7WorkerRequest.FlushCore(Boolean keepConnected, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32[] bodyFragmentTypes) +12233777
System.Web.Hosting.IIS7WorkerRequest.FlushCachedResponse(Boolean isFinal) +847
System.Web.HttpResponse.UpdateNativeResponse(Boolean sendHeaders) +1110
System.Web.HttpRuntime.FinishRequestNotification(IIS7WorkerRequest wr, HttpContext context, RequestNotificationStatus& status) +336


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009

该问题是可重现的,但我没有找到有关此行为的任何信息。如何防止此类问题或如何管理大下载(> 1GB)?

最佳答案

在 IIS 中禁用缓冲将完成这项工作:

public ActionResult DownloadBigFile()
{
string file = @"C:\Temp\File.txt";
var readStream = new FileStream(file, FileMode.Open, FileAccess.Read);
Response.BufferOutput = false; //<-----
return File(readStream, "text/plain", "FILE");
}

为什么这不是返回文件时默认的 ASP.Net MVC 行为,这真的让我感到震惊。尤其是在使用流时。
  • Related thread in forums.iis.net
  • MSDN: HttpResponseBase.BufferOutput
  • 关于返回大于 1 GB 的大文件时,算术运算中的 ASP.NET 溢出或下溢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22289200/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com