最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ASP.NET Core 2.0 MVC file upload size limit - Stack Overflow

programmeradmin3浏览0评论

I'm having trouble with uploading large files in a ASP.NET Core 2.0 MVC web app.

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

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();

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)

I'm access the uploaded files using Request.Form.Files

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:

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.

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.

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.

Update 2: I've just updated all ASP.NET Core and all ASP.NET Core nuget packages to 2.1, but the problem still exists.

I'm having trouble with uploading large files in a ASP.NET Core 2.0 MVC web app.

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

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();

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)

I'm access the uploaded files using Request.Form.Files

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:

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.

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.

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.

Update 2: I've just updated all ASP.NET Core and all ASP.NET Core nuget packages to 2.1, but the problem still exists.

Share Improve this question edited Dec 21, 2024 at 11:39 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Jun 2, 2018 at 8:50 SladeSlade 2,4533 gold badges23 silver badges27 bronze badges 2
  • There should be a 413 and not 500 if the web server cannot handle the file because it is too big. – Andreas Commented Jun 2, 2018 at 9:07
  • @Andreas Yes, this has lead me to believe that the file size is not the problem. However, it still only occurs on large files. – Slade Commented Jun 2, 2018 at 9:20
Add a comment  | 

2 Answers 2

Reset to default 10

To help anyone else with the same problem I have found the answer here:

Max upload size for ASP.NET Core MVC 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.

To solve this problem you can use this code below instead

[DisableRequestSizeLimit]

发布评论

评论列表(0)

  1. 暂无评论