[英]ASP.NET Core MVC: IFormFile return Null when uploaded file is large
I have a student project for C# Course implemented in ASP.NET Core MVC.我有一个用 ASP.NET Core MVC 实现的 C# 课程的学生项目。 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.它的文件上传网站,较小的文件可以完美上传(65 mb,或多或少),但大文件,例如 600 mb 电影,
IFormFile
为空。 I use .NET 6.0.我使用.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上传逻辑在 MovieController -> https://dpaste.org/PBgks (变量“file”为空)中实现,CSHTML 视图上传文件在此处 -> 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. PS:如果我忘记提及解决此问题的一些关键信息,请随时提醒我。
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).更新#1:在对较大的文件重复上传过程后,它会神奇地上传(我不得不点击上传按钮十几次)。 It's really weird.
这真的很奇怪。
Try to find a way to change the maxRequestLength
property.尝试找到一种方法来更改
maxRequestLength
属性。 Although I don't think the problem with it when the default value is only 4MB or has been changed by you before.虽然我不认为默认值只有 4MB 或您之前已更改过它的问题。
Did you try to apply RequestFormLimits
attribute and set the MultipartBodyLengthLimit , like below?您是否尝试应用
RequestFormLimits
属性并设置MultipartBodyLengthLimit ,如下所示?
[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
.此外,如果您在 IIS 下运行,请在
web.config
中设置maxAllowedContentLength
参数。 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.对于 ASP.NET Core MVC 项目,此文件不会自动添加,但 IIS 仍然需要它并且应该手动添加。
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="4147483648" />
</requestFiltering>
</security>
...
</system.webServer>
Configuring the FormsOptions
parameters through the startup.cs
file:通过
startup.cs
文件配置FormsOptions
参数:
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.未提供
_storageServices.UploadFile
的代码。 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:如果您打算将其上传到某个
dir
,请使用以下代码:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.