[SOLVED] Asp.net core web API 6 Doesn't receive IFormFile

Issue

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

Solution

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

Answered By – Ruikai Feng

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *