.net Core 2.0文件上传大小限制 [英] .net Core 2.0 File Upload Size Limit

查看:252
本文介绍了.net Core 2.0文件上传大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.net core 2.0 MVC网络应用上上传大文件时遇到了麻烦。

I'm having trouble with uploading large files in a .net core 2.0 MVC web app.

我看过这篇文章,比如这篇文章,展示了如何增加.net core 2.0中的文件大小限制:
增加Kestrel的上传请求长度限制

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

因此,按照示例我尝试了两个选项。我目前在我的Program.cs中有这个:

So, following the example I have tried both options. I currently have this in my Program.cs:

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

...我在控制器上的ajax文件上传方法如下所示:

... and my ajax file upload method on the controller looks like this:

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

我使用请求访问上传的文件。 Form.Files

视图上的javascript如下所示:

The javascript on the view looks like this:

$('#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:


无法加载资源:服务器响应状态为500()

Failed to load resource: the server responded with a status of 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.

小文件大小一切正常,但当我尝试上传大小为538,286 KB的文件时,它将失败。

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.

任何人都可以帮忙吗?

更新:
有关详细信息,上传文件大小时似乎会出现问题无论任何RequestSizeLimit设置如何,都介于122,211 KB和137,840 KB之间,并且它始终存在错误。

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.

更新2:
我刚刚更新了所有.net核心和所有.net核心nuget包到2.1但问题仍然存在。

Update 2: I've just updated all .net core and all .net core nuget packages to 2.1 but the problem still exists.

推荐答案

为了帮助其他有同样问题的人,我在这里找到了答案:
ASP.MVC CORE网站的最大上传大小

To help anyone else with the same problem I have found the answer here: 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 可在<$中找到c $ c> Microsoft.AspNetCore.Http.Features 命名空间。

这篇关于.net Core 2.0文件上传大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆