How to use fetch to upload files to web api(parameter problem)
My aspnetcore web api side is
[ApiController]
[Route("api/[controller]")]
public class SoundController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> OnPostUploadAsync(List<IFormFile> files0)
{
var files = Request.Form.Files;
.....
return Ok(new { count = files.Count, size });
}
}
My script to post file is as follows:
c开发者_开发问答onst blob = new Blob(chunks, { type: "audio/mp3" });
let formData = new FormData();
formData.append("abcd", blob, "a.mp3");
let response = await fetch("api/sound", {
method: 'post',
body: formData
});
if (response.ok) {
var data = await response.json();
console.log(`response.status=${response.status}, data.count==${data.count},
data.size=${data.size}`);
}
My question is:
Although fetch DO reach the OnPostUploadAsync function,
- but the parameter files0 is of length 0.
- hower inside OnPostUploadAsync function, we can use Request.Form.Files to get the file uploaded by fetch.
- what am I wrong? parameter type issue?
精彩评论