I'm trying to upload/store images on disk memory using express and multer. My code in a nutshell below:
const express = require('express')
const multer = require('multer');
const upload = multer({
destination: function(req, file, cb) {
cb(null, 'uploads/')
},
filename: function(req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
});
app.post('/', upload.single('photo'), function(req, res) {
console.log(req.file);
});
Unfortunately, when I use Postman to send a POST request (the image has the correct fieldname of "photo"), the filename function does not seem to affect the name. The file ends up in /uploads, but with the default naming scheme from multer (not my custom name defined in the filename function).
Also, my console.log(req.file) line outputs this:
{ fieldname: 'photo',
originalname: 'test.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f ... >,
size: 85404 }
Note that there is no value defined for req.file.filename, req.file.destination or req.file.path as specified in the multer API documentation.
Any idea what I'm doing wrong here?