IFormFile always return null in asp.net core 2.1

2020-03-21 02:25发布

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 enter image description here

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

10条回答
戒情不戒烟
2楼-- · 2020-03-21 03:00

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)
查看更多
Ridiculous、
3楼-- · 2020-03-21 03:01

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}`
          }
        });
查看更多
【Aperson】
4楼-- · 2020-03-21 03:02

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'
查看更多
Emotional °昔
5楼-- · 2020-03-21 03:03

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);
查看更多
登录 后发表回答