Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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.

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 .net core and all .net core nuget packages to 2.1 but the problem still exists.

like image 899
Slade Avatar asked Jun 02 '18 08:06

Slade


People also ask

How do I upload a large file to .NET core?

You will need to right click your project in Solution Explorer, then select Add then New Item from the Context menu. Then from the Add New Item Dialog window, select Web Configuration File option and click Add Button. Finally, in the Web. Config file, set the maxAllowedContentLength setting to 100 MB as shown below.

What is the maximum file size for file uploading?

The maximum file size limit for uploads to Box varies depending on your account type: Free personal: 250 MB. Starter: 2 GB. Business: 5 GB.

How do you upload a 5 GB file through your spa to Web API?

After our Web API loaded, we can come to postman tool and using POST method we can send a request to Web API. We must choose “form-data” in the body part and choose “File” as type. We can click the “Send” button now. After some time, we will get a result.

How can I increase the maximum file size I can upload?

Using Plugin MethodGo to your WordPress Dashboard → Plugins → Add new, search “Increase Max Upload Filesize”, then Install and Activate the plugin. Once installed, go to plugin settings and simply enter the value for upload size. Click the Save Changes button to apply the new upload size.


2 Answers

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 can be found in the Microsoft.AspNetCore.Http.Features namespace.

like image 111
Slade Avatar answered Oct 20 '22 18:10

Slade


To solve this problem you can use this code below instead

[DisableRequestSizeLimit]

like image 30
Pericles Radael Avatar answered Oct 20 '22 17:10

Pericles Radael