简体   繁体   English

ASP.NET Core MVC:上传的文件很大时,IFormFile 返回 Null

[英]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.

相关问题 IFormFile 在 ASP.NET Core 3.0 MVC 中始终为 NULL - IFormFile is always NULL in ASP.NET Core 3.0 MVC IFormFile 始终为空(带有 MVC/Razor 的 ASP.NET Core) - IFormFile always null (ASP.NET Core with MVC/Razor) IFormFile 在 asp.net core 2.1 中总是返回 null - IFormFile always return null in asp.net core 2.1 如何在ASP.NET 5 RC1 MVC中使用IFormFile保存上传的文件 - How to save uploaded file using IFormFile in ASP.NET 5 RC1 MVC 如何在asp.net MVC Core的IFormFile中转换byte []? - How to convert byte[] in IFormFile in asp.net MVC Core? IFormFile 在 ASP.NET Core 3.0 中返回 NULL - IFormFile is returning NULL in ASP.NET Core 3.0 在 ASP .NET Core 中更改上传的 IFormfile 的名称 - Change name of uploaded IFormfile in ASP .NET Core 如何在 ASP.NET CORE 中将文件路径转换为 ​​IFormFIle? - How to convert a file path to IFormFIle in ASP.NET CORE? 使用 Asp.NET Core 3.1 框架将文件上传到服务器时如何使用 IFormFile 作为属性? - How to use IFormFile as property when uploading file to a server using Asp.NET Core 3.1 framework? ASP.NET Core Web Application using .NET 5.0: IFormFile always null when passing from view to controller - ASP.NET Core Web Application using .NET 5.0: IFormFile always null when passing from view to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM