I'm trying to post a model with an image file and other data from my PWA to my ASP.NET Core Web Api. I succeed with the Api and Postman, but I can't get it working from the PWA. Furthermore, the model should include an IFormFile but the InputFile control returns an IBrowserFile which I cannot cast.
This is a simplified model:
public class ImageUploadModel { public string Details { get; set; } public IFormFile Image { get; set; } } This is the simplified Api controller:
[HttpPost] public async Task<IActionResult> UploadModel([FromForm] ImageUploadModel imageUploadModel) { var details = imageUploadModel.Details; var image = imageUploadModel.Image; } and here the index.razor test page on the blazor PWA:
@page "/" <div> <InputFile OnChange="@ChangeHandler" /> </div> <div> <button @onclick="Upload">Upload</button> </div> @code { public IBrowserFile? image; private void ChangeHandler(InputFileChangeEventArgs e) { image = e.File; } private async Task Upload() { var model = new imageUploadModel(){ Details = "foo", Image = image } using (var httpClient = new HttpClient()) { using (var response = await httpClient.PostAsJsonAsync("...api url...", model)) { response.EnsureSuccessStatusCode(); apiResponse = await response.Content.ReadAsStringAsync(); } } } }