IFormFile always return null in asp.net core 2.1

2020-03-21 02:17发布

问题:

Api method is looks like below

    [HttpPost]
    public async Task<BaseListResponse<MediaStorageModel>> MediaBrand(IFormFile file, int brandId)
    {
        var files = new List<IFormFile>();
        files.Add(file);

        var response = await this.Upload(files, "brand", brandId);

        return response;
    }

My postman configuration

Upgrade my dotnet core from 2.0 to 2.1 thie become not working, can anyone help about this. What going wrong

回答1:

I've faced the same issue, I was able to fix it by applying the 'Name' named parameter to FromForm attribute with name of the File field in the form. It specifies which field in the form to bind to the method parameter. Change your method signature as shown here.

[HttpPost("status")]
public async Task<BaseListResponse<MediaStorageModel>> MediaBrand([FromForm(Name ="file")] IFormFile file, int brandId)


回答2:

Make sure the form is the correct enctype

<form asp-action="Edit" enctype="multipart/form-data">

I also had to change how the Model bind from the generated

public async Task<IActionResult> Edit([Bind("Text,Example")] Example example)

to

public async Task<IActionResult> Edit(Example example)


回答3:

Adding (Name = "body") to the from form worked for me

Server Call:

[HttpPost]
  [Route("UploadImage")]

public IActionResult UploadImage([FromForm(Name = "body")]IFormFile formData)

Client code:

let formData = new FormData();
formData.append('body', event.target.files[0]);

const config = {
  headers: {
  'content-type': 'multipart/form-data',
  },
}

axios.post(ApiURL,formData, config);


回答4:

In your form use

enctype="multipart/form-data"



回答5:

Change your method argument to take below model and add [FromForm], it should work.

public class FileUploadViewModel
{
    public IFormFile File { get; set; }
    public int BrandId { get; set; }
}

public async Task<BaseListResponse<MediaStorageModel>> MediaBrand([FromForm] FileUploadViewModel viewModel)


回答6:

The below code should work

[HttpPost]
public async Task<BaseListResponse<MediaStorageModel>> MediaBrand([FromQuery] int brandId, IFormFile file)
{
    var files = new List<IFormFile>();
    files.Add(file);

    var response = await this.Upload(files, "brand", brandId);

    return response;
}


回答7:

I have found a workaround to make it work:

Use HttpPut instead of HttPost on the controller action.

I was also surprised by this behavior. If someone can explain why it fixes the issue, it would help me.



回答8:

Update [FromForm] attribute, and don't put parameter into Headers, and put name of key is file and brandId.

I tested, It is Ok



回答9:

If you use javascript and FormData object you need set the name of each file to 'files'

this.files.forEach((f) => {
         formData.append("files", f, `${this.path}/${f.name}`);
      }); 

if you use other name in your post you need to set it in the attribute of the post method

formData.append("someName", f, `${this.path}/${f.name}`);

 public async Task<IActionResult> Post([FromForm(Name ="someName")] List<IFormFile> files)

Dont forget to set content-type

'Content-Type': 'multipart/form-data'


回答10:

In my case, i had an angular 6 app using a custom HttpInterceptor which adds content-type of "application/json" to every Http request along with a token before sending to an api. Something like below. Remove the line with 'Content-Type': application/json. Without that none of the solution here works. .Net Core is smarter now to translate whatever object u are sending to the api so far it matches the model u create for the object in angular.

import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class JwtHttpInterceptor implements HttpInterceptor {
  constructor() {}
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const token = localStorage.getItem('token');
      let clone: HttpRequest<any>;
      if (token) {
        clone = request.clone({
          setHeaders: {
            Accept: `application/json`,
            'Content-Type': `application/json`,
            Authorization: `Bearer ${token}`
          }
        });