I'm trying to upload an image and then send it to the server. But turns out I'm sending an empty object to the server. I'm not using any form. Just through input tag. Is the code correct?
html:
<div>
<input type="file" class="fileUpload" name="file"/>
</div>
js:
document.querySelector('.fileUpload').addEventListener("change", function(e){
let formData = new FormData();
formData.append('imageFiles', e.target.files[0])
// for(var v of formData) {
// console.log(v[0]+', '+v[1]);
// } i tried to check here and it gives `imageFiles, [object File]`. This made me
// think there is data so sent it to server.
fetch(`/image`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
}).then(res => console.log(res))
.then(json => console.log(json))
.catch(err => console.log(err));
})
node.js:
app.post('/image', (req, resp) => {
console.log(req.body)// but get empty object on server {}
})