【问题标题】:Exceeding the 2GB file upload limitation in IIS 7.5 and .NET超过 IIS 7.5 和 .NET 中的 2GB 文件上传限制
【发布时间】:2013-05-24 15:44:57
【问题描述】:

我有一个需要上传超过 2GB 的 (.iso) 文件的 Intranet 应用程序。看来 2GB 文件大小有很多限制因素。

  1. IE 中存在浏览器限制,只有 IE 9/10 可以超过 2GB According to Eric Law
  2. httpRuntimemaxRequestLength元素是Int32类型,最大值为2097151,约2GB。

您似乎可以使用 maxAllowedContentLength 将另一个文件大小限制设置为大约 4GB,因为它是 uint 类型,但是当我们仍然受到 maxRequestLength 的 2GB 限制时,这样做有什么好处?

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="4294967295" />
    </requestFiltering>
  </security>
<system.webServer>

谁有上传文件超过 2GB 限制的解决方案?

【问题讨论】:

标签: asp.net iis file-upload


【解决方案1】:

您愿意接受 JavaScript 解决方案吗??如果是这种情况,请尝试this jQuery 插件,它允许您上传大量数据(大量 GB)。 如果浏览器不支持提供基于 TCP/IP 发送和接收具有相应 ACK 的包的机制,它会使用 HTML5 FileReader API 功能和 Silverlight 后备来上传文件。文件按配置大小的块上传(默认为 4 MB)。

另外:它还带有文件队列模式。

以下是如何在 Razor 视图中使用它的示例:

$(function () {

    var file = $("#file").createUploaderHtml5({
        postDataUrl: "@Url.Action("Upload", "Home")",
        packetSize: 4 * 1024 * 1024,
        onPreparingUpload: function (plugin, ufname, umime, usize) {
            plugin.settings.logger("ufname = [" + ufname + "] umime = [" + umime + "] usize = [" + usize + "]");
            return true;
        },
        onInitPacketArrived: function (plugin, guid) {
            plugin.settings.logger("guid = [" + guid + "]");
        },
        onDataPacketArrived: function (plugin, ack, total) {
            //plugin.settings.logger("ACK [" + ack.Guid + "] packet = [" + ack.Packet + "] total = [" + total + "]");
            var percent = Math.round(ack.Packet / total * 100);
            $("#progressbar").attr("value", percent);
            $("#percent").html(percent + " %");
        },
        onFileUploaded: function (pl) {
            pl.settings.logger("File finished!!!");
        },
        logger: function(msg) {
            var lg = $("#logger");
            lg.html(lg.html() + msg + "<br />");
        }
    });

    $("#start").click(function () {
        file.startUpload();
    });

    $("#stop").click(function () {
        file.cancelUpload();
    });

});

这是上传操作的代码:

[HttpPost]
public ActionResult Upload(FormCollection collection)
{
    var packetSize = 4 * 1024 * 1024; // default to 4 MB
    var filePath = Server.MapPath("~/_temp_upload/");

    var result = UploadHelper.ProcessRequest(Request, filePath, packetSize);

        if (result != null)
        {
            var metadata = UploadHelper.GetMetadataInfo(filePath, result.Guid);
            // do anything with the metadata
        }

        if (result != null)
            return Json(result);
        return Content("");
    }

【讨论】:

  • 我对 js 解决方案持开放态度,任何基于 HTTP 的解决方案。我会调查 FreshUpload。
  • 希望对 @m4chine 有所帮助。我将它用于相同的 IIS 限制
  • 你能重新发布那个链接吗?我认为它在您的更新中丢失了...谢谢!
  • uppsss 我的错,链接已在我的答案中修复!有关更多使用示例,请查看bitbucket.org/abelperezok/freshupload
  • nebtrx 你能发布你的 Home/Upload 控制器操作吗?
【解决方案2】:

通过进行以下更改,我能够使用 Web API 和 IIS 最多上传 4 GB。 在 web api 项目中,在 web.config 中进行以下 2 处更改以设置最大长度。

  <requestFiltering>
    <requestLimits maxAllowedContentLength="4294967295"/>
  </requestFiltering>
  <httpRuntime targetFramework="4.5.2" executionTimeout="2400" maxRequestLength="2147483647"/>

在调用 web api 时在客户端添加分块标头,如下所示,这会阻止 IIS 通过流式传输文件来限制超过 2 GB 的文件-

HttpClient.DefaultRequestHeaders.Add("Transfer-Encoding", "chunked");

在服务器端(web api 控制器)读取流之前添加以下代码,以使用重载方法 GetBufferlessInputStream(disableMaxLength) 忽略最大请求长度 2 GB-

var content = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true));
foreach(var header in Request.Content.Headers) {
 content.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
await content.ReadAsMultipartAsync(streamProvider);

更改服务器端的策略选择器,以便禁用缓冲并改为流式传输文件。添加下面的类以覆盖您的控制器的 WebHostBufferPolicySelector(例如,下面 sn-p 中的“文件”控制器)-

public class NoBufferPolicySelector: WebHostBufferPolicySelector {
 public override bool UseBufferedInputStream(object hostContext) {
  var context = hostContext as HttpContextBase;

  if (context != null && context.Request.RequestContext.RouteData.Values["controller"] != null) {
   if (string.Equals(context.Request.RequestContext.RouteData.Values["controller"].ToString(), "File", StringComparison.InvariantCultureIgnoreCase))
    return false;
  }

  return true;
 }

 public override bool UseBufferedOutputStream(HttpResponseMessage response) {
  return base.UseBufferedOutputStream(response);
 }
}

将下面添加到注册方法中-

GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferPolicySelector());

希望这对任何人都有帮助。

【讨论】:

    【解决方案3】:

    今年我一直在努力解决从各种浏览器上传到 IIS 服务器的大文件。这是我发现的:

    ASP.NET supports upload over 2Gb since .Net 4.5(可能它支持高达long.MaxValue 的文件)。但是 IIS 本身不支持超过 2Gb 的上传。所以任何托管在 IIS 中的服务器都不支持超过 2Gb 的上传。

    据我了解,将 maxAllowedContentLengthmaxRequestLength 设置为超过 2Gb 的值并没有帮助,因为这些设置适用于 ASP.NET,而核心问题在于 IIS。

    【讨论】:

    • 我想这里要推断的“答案”是使用IIS以外的服务器?
    猜你喜欢
    • 2011-10-11
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 2014-12-21
    • 2018-02-14
    相关资源
    最近更新 更多