1

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

1 Answer 1

2

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))
1
  • simply your file(send) name should be exact same thing in your api input. Apr 7, 2022 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.