Home > Enterprise >  Asp.net core web API 6 Doesn't receive IFormFile
Asp.net core web API 6 Doesn't receive IFormFile

Time:03-15

my client sends the file successfully but my API doesn't receive the file

here is my client code

 protected async Task Send(string url, HttpMethod method, IFormFile file )
    {

        var accessToken = await _mService.GetAccessTokenAsync();
        using var request = new HttpRequestMessage(method, url);
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        

        using var content = new MultipartFormDataContent
        {
            { new StreamContent(file.OpenReadStream()), file.Name, file.FileName }
        };
        request.Content = content;
        
        using var response = await _httpClient.SendAsync(request);
        
        response.EnsureSuccessStatusCode();
}

and my request enter image description here

enter image description here

and my server API is

 [HttpPost("User/{providerId}/AdsImage/{adsImageId}")]
[Consumes("multipart/form-data")]
public async Task<IActionResult> UploadNewAdsImage( [FromForm] IFormFile uploadFile,[FromRoute] Guid providerId , [FromRoute] Guid adsImageId)
{
}

but the only the file is null enter image description here

enter image description here

CodePudding user response:

The key name of the file that you added to the content was different from"uploadfile",so you couldn't bind the model

You could modify your codes:

var content = new MultipartFormDataContent
content.Add(new StreamContent(file.OpenReadStream()), "uploadfile",   Path.GetFileName(file.FileName))
  • Related