I am trying to upload images to an Express server. I'm not quite sure how to do this, but here is what I got from what I managed to get from MDN, express , react-dropzone and multer Documentation. It seems that Multer does not take the FormData object from react-dropzone , when req.file it, it returns undefined.
server.js
var storage = multer.diskStorage({ destination: './public/users', filename: function (req, file, cb) { switch (file.mimetype) { case 'image/jpeg': ext = '.jpeg'; break; case 'image/png': ext = '.png'; break; } cb(null, file.originalname + ext); } }); var upload = multer({storage: storage}); app.use(upload.single('photo')); app.post('/uploadUserImage', function (req, res) { console.log(JSON.stringify(req.body.photo))
client.js
function uploadImage (image) { var formData = new FormData(); formData.append('photo', image); fetch('http://localhost:8080/uploadUserImage/', { method:'POST', body: formData }); }
When I make this morgan request, the following is issued:
{photo: '[object File]'} <--- from console.log (req.body ');
undefined <--- from console.log (req.file);
multer creates a public/uploads , but does not put the image in a location. How can I get a photo because I need to run it through sharp (to compress the file size and resize the image) and then put it in the uploads folder?
guyforlowbro
source share