I have a student project for C# Course implemented in ASP.NET Core MVC. Its file upload website, smaller files uploads flawlessly (65 mb, more or less) but large files as for example, 600 mb movie the IFormFile
is null. I use .NET 6.0.
Uploading logic is implemented in MovieController -> https://dpaste.org/PBgks (a variable "file" is null) and CSHTML view uploadfile is here -> https://dpaste.org/25mOS
My project passed but I want to solve this bug once and for all
PS: If I forgot to mention some crucial info to solve this problem, please feel free to remind me.
Sorry for my rusty English.
UPDATE #1: After repeating a uploading process with larger files, it magically uploads (I had to click on upload button a dozen times). It's really weird.
Try to find a way to change the maxRequestLength
property. Although I don't think the problem with it when the default value is only 4MB or has been changed by you before.
Did you try to apply RequestFormLimits
attribute and set the MultipartBodyLengthLimit , like below?
[RequestFormLimits(MultipartBodyLengthLimit = 6104857600)]
public async Task<IActionResult> UploadFile(IFormFile file, int id)
{
// your core here...
}
In additional, if you are running under the IIS set maxAllowedContentLength
parameter in the web.config
. For the ASP.NET Core MVC project this file doesn't added automatically, but it still needed for the IIS and should be added manually.
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="4147483648" />
</requestFiltering>
</security>
...
</system.webServer>
Configuring the FormsOptions
parameters through the startup.cs
file:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<FormOptions>(o =>
{
o.ValueLengthLimit = int.MaxValue;
o.MultipartBodyLengthLimit = long.MaxValue;
});
services.AddControllersWithViews();
}
code for _storageServices.UploadFile
is not provided. though i think you code:
public async Task<IActionResult> UploadFile(IFormFile file, int id)
{
var viewMod = new UploadViewModel();
viewMod.Id = id;
try
{
await _storageServices.UploadFile(file, id).ConfigureAwait(false);
ViewBag.Message = "File Uploaded Successfully!!";
return View(viewMod);
}
catch
{
ViewBag.Message = "File upload failed!!";
return View(viewMod);
}
}
looks like you are just passing it to the service which then updates the database.
if your intention is to upload it to some dir
then use this code:
try
{
//this will upload the file to directory
using (var stream = new FileStream("upload path", FileMode.Create))
{
await file.CopyToAsync(stream);
}
//your code for entry of file in database
await _storageServices.UploadFile(file, id).ConfigureAwait(false);
ViewBag.Message = "File Uploaded Successfully!!";
return View(viewMod);
}
catch
{
ViewBag.Message = "File upload failed!!";
return View(viewMod);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.