I have tried several things to get my image data from the file input and send it to the NodeJS server but nothing has helped me so far. I had a lot of different solutions with formData, FileReader etc. but I never had my data.
My HTML is following:
<button type="button" id="uploadButton" onclick="openFilepicker()">
Upload file
</button>
The openFilepicker() function triggers a click via jQuery on the file-input element:
<form id='uploadForm' encType="multipart/form-data">
<input type="file" accept="image/*" name="uploader" class="hidden"
id="input-file" onchange="submitUploadForm()"/>
</form>
The submitUploadForm() function gets executed when a file has been selected:
function submitUploadForm() {
if ($('#input-file').val() != '') {
const input = document.getElementById('input-file');
const file = input.files[0];
/* Process my data here to be ready to send it */
$.ajax({
url: '/upload',
type: 'POST',
data: '', /* Here I want my file data to be sent */
cache: false,
contentType: false,
processData: false,
success: function (data) {
console.log(data);
}.bind(this),
error: function (err) {
console.log(err);
}.bind(this)
});
}
}