This question might be a duplicate, in that case I would love to get a reading on it, but please check if the duplicate question fits mine. I have tried looking for answers, but have not found any that fits my question correctly.
I have a website built with React served from a .NET Core 2.0 project with a regular Web API generated from the regular Controller web api that is built in to the project. The Web API is set up like this:
[Produces("application/json")]
[Route("api/File")]
public class FileController : Controller
{
// POST: api/File
[HttpPost]
public ActionResult Post()
{
Console.WriteLine(Request);
return null;
}
I want to upload Images / PDF files and other file types from a regular input type="file" field.
The code for that can be seen below:
export class Home extends Component {
render() {
return <input type = "file"
onChange = {
this.handleFileUpload
}
/>
}
handleFileUpload = (event) => {
var file = event.target.files[0];
var xhr = new XMLHttpRequest();
var fd = new FormData();
xhr.open("POST", 'api/File', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status == 200) {
// Every thing ok, file uploaded
console.log(xhr.responseText); // handle response.
}
};
fd.append("upload_file", file);
xhr.send(fd);
}
}
What needs to be implemented in the Post-file-controller part for the correct handling of the file? If I want the file to be uploaded as, say a uint8 array (to be stored).
Every bit of help is appreciated as I am stuck.