对于我正在开发的应用程序,我需要允许用户通过我们的网站上传非常大的文件——也就是说,可能有很多GB.不幸的是,ASP.NET MVC似乎在开始服务之前将整个请求加载到RAM中——这对于这样的应用程序来说并不理想.值得注意的是,试图通过以下代码规避该问题:

if (request.Method == "POST")
{
    request.ContentLength = clientRequest.InputStream.Length;
    var rgbBody = new byte[32768];

    using (var requestStream = request.GetRequestStream())
    {
        int cbRead;
        while ((cbRead = clientRequest.InputStream.Read(rgbBody, 0, rgbBody.Length)) > 0)
        {
            fileStream.Write(rgbBody, 0, cbRead);
        }
    }
}

未能绕过缓冲区将请求转换为RAM.有没有一种简单的方法来解决这种行为?

推荐答案

事实证明,我最初的代码基本上是正确的;唯一需要的改变就是改变

request.ContentLength = clientRequest.InputStream.Length;

request.ContentLength = clientRequest.ContentLength;

The former streams in the entire request 至 determine the content length; the latter merely checks the Content-Length header, which only requires that the headers have been sent in full. This allows IIS 至 begin streaming the request almost immediately, which completely eliminates the original problem.

Asp.net相关问答推荐

self 跟踪实体与 POCO 实体

无法获取项目引用的依赖项

有没有办法在外部 javascript 文件中使用<%= someObject.ClientID %>?

如何计算/查找给定日期的周数?

HttpContext.Current.Request.IsAuthenticated 和 HttpContext.Current.User.Identity.IsAuthenticated 有什么区别?

如何将 ASP.NET MVC5 身份验证添加到现有数据库

更改后未应用 CSS

ASP.NET 中的多选下拉列表

Owin 中是否有来自 Global.asax 的 Application_End?

根据 DropDownList 的值动态启用或禁用RequiredFieldValidator

有条件地排除 ASP.NET WebForms 中的一个 html 块

*.csproj 中的 usevshostingprocess 是什么?

ASP.NET:在 Response.Redirect(...) 之后代码会发生什么?

在 ASPNET 标识 2.0 中禁用用户

在 ASP.NET 中获取服务器的 IP 地址?

System.Web.Caching 还是 System.Runtime.Caching 更适合 .NET 4 Web 应用程序

ASP.NET 网格视图与列表视图

如何使用 int ID 列更改 ASP.net Identity 2.0 的表名?

新的 csproj 文件 struct 可以与 ASP.NET Framework 项目一起使用吗?

ASP.NET 自定义控件 - 未知的服务器标记