'Razor Pages .Net Core - cannot upload large Files / Videos
I am trying to 'upload' a video file in to my application and pass it to the database. I am able to handle pictures quite nicely however as soon as I try larger / video files I am getting a Status: 400 error code when a Submit is made.
I put a break point on the OnPostAsync
method in code behind but it doesn't get hit.
I just have a standard Input --> Type: File:
and from seeing a few mentions online I have manually added a web.config
file to the project and added the following:
However I still get the 400 error if I attempt to upload a large file. I notice in the actual call it should allow the mime type:
As per other advice I have also tried adding the following to the
but get the following:
And also on the model that is being populated with the large file data:
Still the same error.
I would be interested to hear peoples suggestions on what could be causing my error.
Solution 1:[1]
I have managed to get this working eventually by adding the following to the Startup.cs file:
Code:
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
x.MultipartHeadersLengthLimit = int.MaxValue;
});
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = int.MaxValue;
});
Now I am able to load large files as expected.
Solution 2:[2]
Add this line just before the method where you try to upload a large file.
[RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
Solution 3:[3]
In ASP.NET Core MVC, You can put the below code (Attribute) before your method (Action) but in Razor pages, We can not put it before method (Handler)! and the compiler will notify warning and the code does not work!
[RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
So, In ASP.NET Core Razor Pages, We should put the below code (Attribute) before the class (PageModel) not before the method (Handler)!
[RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
public class UploadFile : PageModel
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CJH |
Solution 2 | Pritom Sarkar |
Solution 3 | Dariush Tasdighi |