开发者

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,

  1. but the parameter files0 is of length 0.
  2. hower inside OnPostUploadAsync function, we can use Request.Form.Files to get the file uploaded by fetch.
  3. what am I wrong? parameter type issue?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜