简体   繁体   中英

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. I would appreciate any help. No matter what I do, my IFormFile is always null. Here is the MVC setup:

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" ).

The Input Tag Helper binds an HTML <input> element to a model expression in your razor view are-

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

Generates the id and name HTML attributes for the expression name specified in the asp-for attribute. asp-for="Property1.Property2" is equivalent to m => m.Property1.Property2 . The name of the expression is what is used for the asp-for attribute value.

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 :

<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. 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.

took me over an hour to figur out that i forgot Multipart/form-data

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

just posting this if anyone ever run into this problem again

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.

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