FilePathResult使用大文件抛出OutOfMemoryException

 yigoon 发布于 2023-02-13 19:50

我在我的控制器中有这个Action,它将一个文件返回给用户.

public virtual ActionResult ReturnFile(string fileName, string filePath, string contentType)
{
    var cd = new System.Net.Mime.ContentDisposition
    {
        FileName = fileName,

        // always prompt the user for downloading, set to true if you want 
        // the browser to try to show the file inline
        Inline = false,
    };

    // set token for close the modal-window
    CookiesHelper.SetValueDownloadTokenInCookie();
    Response.AppendHeader("Content-Disposition", cd.ToString());
    return File(filePath, contentType);
}

它工作正常,但问题是,当文件很大(略大于220 Mb)时,它会抛出一个OutOfMemoryException.

这是堆栈跟踪

[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
   System.IO.MemoryStream.set_Capacity(Int32 value) +93
   System.IO.MemoryStream.EnsureCapacity(Int32 value) +64
   System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +330
   Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.ArteryFilter.Write(Byte[] buffer, Int32 offset, Int32 count) +106
   System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +9509748
   System.Web.HttpResponse.FilterOutput() +104
   System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +49
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

有任何想法吗?

1 个回答
  • 这是因为您的请求正在使用PageInspector堆栈跟踪的这一行所示:

    Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.ArteryFilter.Write(Byte [] buffer,Int32 offset,Int32 count)+106

    要解决此问题,请取消选中浏览器复选框,Visual Studio如以下屏幕截图所示:

    取消选中浏览器链接

    2023-02-13 19:53 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有