onurmhb onurmhb - 1 year ago 184
ASP.NET (C#) Question

How to upload files with react to my .net server

How do I upload images to my server. I am using react and so far my controller looks like this:

public object UploadAvatar()
var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;

file shows = null when I am upload with react and axios:

const URL = '/api/userapi/uploadavatar';
var reader = new FileReader();
var file = e.target.files[0];
reader.onload = (upload) => {
axios.post(URL, {
data: upload.target.result
}, config);

But when i upload with postman it works fine

Answer Source

You can just post the file object like this:

const URL = '/api/userapi/uploadavatar';
var file = e.target.files[0];
axios.post(URL, {data: file});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download