简体   繁体   中英

Upload Files via JS to ASP.Net Core with IFormFile

I am trying to send a file and other data to an Asp-Core-BackEnd.

In the backendController, I read the file like:

IFormFile file = HttpContext.Request.Form.Files.First(); //don't work

HttpContext.Request.Form.TryGetValue("id", out id)  //works

Works for other Clients. But I want use the Api with JS.

My js-code (react) looks like this:

const formData = new FormData();      
formData.append('id', 123);
formData.append('files', files);  //don't work

const config = {
    headers: {
      'content-type': 'multipart/form-data; charset=utf-8; boundary="------";'
    }
  }
        
await axios.post(url, formData, config);

but Form.Files is an empty Array

Try this?

[HttpPost]
        public string getfile([FromForm] string id, [FromForm] IFormFile file) {
            return "success";
        }

在此处输入图片说明 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM