Uploading images with fetch to Express using Multer
The error occurs because you specified the 'Content-type'
explicitly. To do this properly, you'd also need to specify the boundary. You can find a detailed explanation of multipart/form-data
here: How does HTTP file upload work?
To solve the issue with the file upload, you should remove the 'Content-Type'
specification from the fetch
request. You can also refactor the uploadImage
method to upload the form without parsing the inputs:
function uploadImage () { // This assumes the form's name is `myForm` var form = document.getElementById("myForm"); var formData = new FormData(form); fetch('http://localhost:8000/uploadUserImage', { method: 'POST', body: formData });}
The problem for me was that firebase has an error and can't use multer. You need to use busboy and parse it manually. Also I needed to append the uri from react native imagePicker instead of the file blob. Like this:
data.append('fileData', { uri : pickerResponse.uri, type: pickerResponse.type, name: pickerResponse.fileName});