Jens B
Jens B

Reputation: 151

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

Upvotes: 1

Views: 4326

Answers (1)

Tiny Wang
Tiny Wang

Reputation: 15916

Try this?

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

enter image description here enter image description here

Upvotes: 3

Related Questions