简体   繁体   English

IFormFile 在 ASP.NET Core 3.0 MVC 中始终为 NULL

[英]IFormFile is always NULL in ASP.NET Core 3.0 MVC

I have been banging my head on this one for almost 2 days now.我已经在这个问题上敲了将近 2 天了。 I would appreciate any help.我将不胜感激任何帮助。 No matter what I do, my IFormFile is always null.无论我做什么,我的 IFormFile 始终为空。 Here is the MVC setup:这是 MVC 设置:

Razor View:剃刀视图:

<form method="post" enctype="multipart/form-data" asp-controller="Admin" asp-action="LoadProvidersBulk">
    <h6 class="text-muted">Upload Provider File</h6>
    <h6 class="text-muted small">Supported File Types: <b>.xlsx</b></h6>

    <div class="form-group-row d-inline-flex badge-dark" style="padding-top:10px; padding-bottom:10px;">
        <div class="col-sm-4" style="padding:2px">
            <input type="file" asp-for="FormFile" name="ProviderUpload" class="form-control-sm" />
        </div>
    </div>
    <div class="form-group row" style="padding-top:10px">
        <div class="col-sm-5 text-left">
            <button class="btn btn-sm btn-dark btn-primary" type="submit">Load Providers</button>
            <a asp-action="LoadProviders" class="btn btn-sm btn-dark btn-primary">Cancel</a>
        </div>
    </div>
</form>

Model:模型:

using SampleProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SampleProject.ViewModels
{
    public class LoadProvidersBulkViewModel
    {
        public IFormFile FormFile { get; set; }
    }
}

Controller:控制器:

        [HttpPost]
        public async Task<IActionResult> LoadProvidersBulk([FromForm(Name = "ProviderUpload")] LoadProvidersBulkViewModel loadProvidersBulkViewModel, CancellationToken cancellationToken)
        {
            var file = loadProvidersBulkViewModel.FormFile;            

            if (file == null || file.Length <= 0)
            {
                ModelState.AddModelError("BulkUploadError", "The file cannot be empty");
                return View("LoadProvidersBulk");
            }

            /* Removed all other code for brevity */

            return RedirectToAction("Users", _userManager.Users);
        }

You mixed up asp-for and name attribute name ( asp-for="FormFile" name="ProviderUpload" ).您混淆了asp-for和 name 属性nameasp-for="FormFile" name="ProviderUpload" )。

The Input Tag Helper binds an HTML <input> element to a model expression in your razor view are- Input Tag Helper 将 HTML <input>元素绑定到剃刀视图中的模型表达式是 -

<input asp-for="<Expression Name>">

Generates the id and name HTML attributes for the expression name specified in the asp-for attribute.为 asp-for 属性中指定的表达式名称生成 id 和 name HTML 属性。 asp-for="Property1.Property2" is equivalent to m => m.Property1.Property2 . asp-for="Property1.Property2"相当于m => m.Property1.Property2 The name of the expression is what is used for the asp-for attribute value.表达式的名称用于asp-for属性值。

So your code look like-所以你的代码看起来像 -

View:-看法:-

@model SampleProject.ViewModels.LoadProvidersBulkViewModel


<form method="post" enctype="multipart/form-data" asp-controller="Admin" asp-action="LoadProvidersBulk">
    <h6 class="text-muted">Upload Provider File</h6>
    <h6 class="text-muted small">Supported File Types: <b>.xlsx</b></h6>

    <div class="form-group-row d-inline-flex badge-dark" style="padding-top:10px; padding-bottom:10px;">
        <div class="col-sm-4" style="padding:2px">
            <input type="file" asp-for="FormFile" class="form-control-sm" />
        </div>
    </div>
    <div class="form-group row" style="padding-top:10px">
        <div class="col-sm-5 text-left">
            <button class="btn btn-sm btn-dark btn-primary" type="submit">Load Providers</button>
            <a asp-action="LoadProviders" class="btn btn-sm btn-dark btn-primary">Cancel</a>
        </div>
    </div>
</form>

Controller:-控制器:-

    [HttpPost]
    public async Task<IActionResult> LoadProvidersBulk(LoadProvidersBulkViewModel model)
    {
        var file = model.FormFile;            

        if (file == null || file.Length <= 0)
        {
            ModelState.AddModelError("BulkUploadError", "The file cannot be empty");
            return View("LoadProvidersBulk");
        }

        /* Removed all other code for brevity */

        return RedirectToAction("Users", _userManager.Users);
    }

Just change your action parameter like below:只需更改您的操作参数,如下所示:

  public async Task<IActionResult> LoadProvidersBulk([FromForm] LoadProvidersBulkViewModel loadProvidersBulkViewModel, CancellationToken cancellationToken)

And your input name must be changed to FormFile :并且您的输入名称必须更改为FormFile

<input type="file" asp-for="FormFile" name="FormFile" class="form-control-sm" />

I found the problem.我发现了问题。 I implemented my own interface class for IFormFile instead of using the Microsoft.AspNetCore.Http directive.我为 IFormFile 实现了自己的接口类,而不是使用 Microsoft.AspNetCore.Http 指令。 Once removing that reference from the Model it worked as expected.从模型中删除该引用后,它按预期工作。

Thank you for those that took the time to respond.感谢那些花时间回复的人。

2 years & 2 month later i'm sititng with exact same problem. 2 年和 2 个月后,我遇到了完全相同的问题。

took me over an hour to figur out that i forgot Multipart/form-data我花了一个多小时才弄清楚我忘记了Multipart/form-data

<form asp-action="Create" method="post" enctype="multipart/form-data">

just posting this if anyone ever run into this problem again如果有人再次遇到这个问题,就发布这个

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM