简体   繁体   English

.net Core 2.0 文件上传大小限制

[英].net Core 2.0 File Upload Size Limit

I'm having trouble with uploading large files in a .net core 2.0 MVC web app.我在 .net core 2.0 MVC Web 应用程序中上传大文件时遇到问题。

I have seen articles, such as this one, which shows how to increase the file size limit in .net core 2.0: Increase upload request length limit in Kestrel我看过一些文章,比如这篇文章,它展示了如何在 .net core 2.0 中增加文件大小限制: 增加 Kestrel 中的上传请求长度限制

So, following the example I have tried both options.因此,按照示例,我尝试了两种选择。 I currently have this in my Program.cs:我目前在 Program.cs 中有这个:

        public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel(options => options.Limits.MaxRequestBodySize = null)
            .Build();

... and my ajax file upload method on the controller looks like this: ...我在控制器上的 ajax 文件上传方法如下所示:

    [HttpPost]
    [RequestSizeLimit(1_074_790_400)]
    [Route("api/article/uploadfile/{mediaType}")]
    public async Task<IActionResult> UploadFile(string mediaType)

I'm access the uploaded files using Request.Form.Files我正在使用Request.Form.Files访问上传​​的文件

The javascript on the view looks like this:视图上的 javascript 如下所示:

$('#upload-videos').change(function () {
    var files = $("#upload-videos").get(0).files;
    uploadMedia(false, files);
})

function uploadMedia(isPhoto, files) {
    var type;
    if (isPhoto) {
        type = "i";
    }
    else {
        type = "v";
    }

    var data = new FormData();
    if (files.length > 0) {
        for (idx = 0; idx < files.length; idx++) {
                data.append("fileImage" + idx, files[idx]);
        }

        $.ajax({
            url: "/api/article/uploadfile/" + type,
            type: "POST",
            processData: false,
            contentType: false,
            dataType: false,
            data: data,
            success: function (jsonData) {
                refreshUploadedImages(jsonData, isPhoto);
            }
        });
    }
}

The problem is, even with the changes to increase the upload limit in place, I am get the response:问题是,即使进行了增加上传限制的更改,我也得到了回应:

Failed to load resource: the server responded with a status of 500 ()加载资源失败:服务器响应状态为 500()

If I put a break-point on the first line of the controller method it never hits it so it doesn't appear to be a problem with this code.如果我在控制器方法的第一行放置一个断点,它永远不会命中它,所以这段代码似乎没有问题。

Everything works fine with small files size but when I try to upload a file which is 538,286 KB in size it will fail.对于小文件大小一切正常,但是当我尝试上传大小为 538,286 KB 的文件时,它会失败。

Can anyone help with this?有人能帮忙吗?

Update: For further information, the problem seems to occur when the upload file size is somewhere between 122,211 KB and 137,840 KB regardless of any RequestSizeLimit settings, and it errors consistently.更新:有关更多信息,无论任何 RequestSizeLimit 设置如何,当上传文件大小介于 122,211 KB 和 137,840 KB 之间时,似乎都会出现此问题,并且始终出错。

Update 2: I've just updated all .net core and all .net core nuget packages to 2.1 but the problem still exists.更新 2:我刚刚将所有 .net core 和所有 .net core nuget 包更新到 2.1,但问题仍然存在。

To help anyone else with the same problem I have found the answer here: Max upload size for ASP.MVC CORE website为了帮助其他有同样问题的人,我在这里找到了答案: Max upload size for ASP.MVC CORE website

It turns out that you need to remove the body length limit like this:事实证明,您需要像这样删除主体长度限制:

services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = 1_074_790_400);

FormOptions can be found in the Microsoft.AspNetCore.Http.Features namespace. FormOptions可以在Microsoft.AspNetCore.Http.Features命名空间中找到。

To solve this problem you can use this code below instead要解决此问题,您可以使用下面的代码

[DisableRequestSizeLimit] [禁用请求大小限制]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM