IFormFile always return null in asp.net core 2.1

前端 未结 11 1837
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 11:52

Api method is looks like below

    [HttpPost]
    public async Task> MediaBrand(IFormFile file, int brandId)
         


        
相关标签:
11条回答
  • 2021-01-11 12:11

    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)
    
    0 讨论(0)
  • 2021-01-11 12:12

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

    I tested, It is Ok

    0 讨论(0)
  • 2021-01-11 12:13

    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);
    
    0 讨论(0)
  • 2021-01-11 12:13

    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}`
              }
            });
    
    0 讨论(0)
  • 2021-01-11 12:23

    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;
    }
    
    0 讨论(0)
  • 2021-01-11 12:25

    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 works from the generated code:

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

    to this code:

    public async Task<IActionResult> Edit(Example example)
    
    0 讨论(0)
提交回复
热议问题