简体   繁体   English

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

I am trying to allow the user to upload a pdf file.我正在尝试允许用户上传 pdf 文件。 I am not getting any errors, but the IFormFile 'PostedFile' is always null in the controller.我没有收到任何错误,但 IFormFile 'PostedFile' 在 controller 中始终是 null。

Create View:创建视图:

 <div class="form-group">
      <label asp-for="PostedFile" class="control-label"></label>
      <div class="col-md-10">
           <input type="file" asp-for="PostedFile" />
           <span asp-validation-for="PostedFile" class="text-danger"></span>
      </div>

Controller, Create method: Controller,创建方法:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Name,Phone1,Phone2,District_Division,OrgNumber,DateOfTest,DateOfExposure,NumberOfExposed,Notes,PathToFile")] Case_Log case_Log, IFormFile PostedFile)
{
    string path = "Case_Log_Docs/";
    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);
        System.Diagnostics.Debug.WriteLine("Created the folder.");
    }

    if (PostedFile != null)
    {
        string fileName = Path.GetFileName(PostedFile.FileName);
        System.Diagnostics.Debug.WriteLine(fileName);
        PostedFile.CopyTo(new FileStream(path, FileMode.Create));
        ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("Posted file was null.");
    }

    if (ModelState.IsValid)
    {
        _context.Add(case_Log);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    return View(case_Log);
}

PLEASE NOTE: I (think I) do NOT want to use List as I do NOT want the user to be able to upload more than 1 single document at a time as the documents have a corresponding database entries with a 1 to 1 relationship.请注意:我(我想我)不想使用列表,因为我不希望用户一次能够上传超过 1 个文档,因为这些文档具有对应的数据库条目,并且具有 1 对 1 的关系。

I have a few questions.我有几个问题。

1.) What is the problem? 1.) 有什么问题? Why is IFormFile always null?为什么 IFormFile 总是 null? 2.) Why does it seem like people always recommend List over IFormFile? 2.) 为什么人们似乎总是推荐 List 而不是 IFormFile?

Passing the rest of the variables to the controller works fine:将变量的 rest 传递给 controller 可以正常工作:

<form asp-action="Create">
    <div asp-validation-summary="ModelOnly" class="text-danger"> </div>
    <div class="form-group">
        <label asp-for="Name" class="control-label"></label>
        <input asp-for="Name" class="form-control" />
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="Phone1" class="control-label"></label>
        <input asp-for="Phone1" class="form-control" />
        <span asp-validation-for="Phone1" class="text-danger"></span>
    </div>

But the file upload div is still inside of the form that is directed to the Create method.但是文件上传 div 仍然在指向 Create 方法的表单内。 Is there something wrong with the view element?视图元素有问题吗? If so how would I change it to correct the issue?如果是这样,我将如何更改它以纠正问题?

I tried following this example and got no errors but no results either: https://www.aspsnippets.com/Articles/ASPNet-Core-IFormFile-always-returns-NULL.aspx我尝试按照此示例进行操作,没有错误,但也没有结果: https://www.aspsnippets.com/Articles/ASPNet-Core-IFormFile-always-returns-NULL.aspx

You need use enctype=multipart/form-data to allows entire files to be included in the data.Like following.您需要使用enctype=multipart/form-data来允许将整个文件包含在数据中。如下所示。

<form asp-action="xxx" enctype="multipart/form-data">
 //...
    <input type="file" name="PostedFile" />
    <input type="submit" value="click"/>
</form>

Action:行动:

[HttpPost]
public IActionResult Demo(IFormFile PostedFile)
 {
    //...
 }

Result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IFormFile 在 asp.net core 2.1 中总是返回 null - IFormFile always return null in asp.net core 2.1 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) ASP .NET Core Web API - Getting and extracting a.zip file from upload controller, using IFormFile - ASP .NET Core Web API - Getting and extracting a .zip file from upload controller, using IFormFile 将两个或多个参数从 model 视图传递到 EF Core / ASP.NET Core MVC 应用程序中的 controller - Passing two or more parameters from model view to controller in EF Core / ASP.NET Core MVC application IFormFile 在 ASP.NET Core 3.0 中返回 NULL - IFormFile is returning NULL in ASP.NET Core 3.0 ASP.NET Core MVC:上传的文件很大时,IFormFile 返回 Null - ASP.NET Core MVC: IFormFile return Null when uploaded file is large c# asp.net core mvc 将数据从视图传递到控制器并从控制器返回到视图 - c# asp.net core mvc passing data from view to controller and from controller back to view 使用HttpClient Alwyes Null ASP.net Core2.2将IFormFile发送到API - Send IFormFile To API using HttpClient Alwyes Null ASP.net Core2.2 无法在ASP.Net Core 2中使用IFormFile对象上载具有空引用接收的照片 - Could not upload photo using IFormFile object in ASP.Net Core 2 with null reference expception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM