Reputation: 1149
I have some problems with my server nodejs when i upload a image by multer. https://github.com/expressjs/multer When i go to folder uploads, i don't see my file uploaded!
My code:
app.use(multer(
{
dest: path.join(__dirname, 'uploads'),
inMemory: true,
includeEmptyFields: true,
onFileUploadStart: function (file, req, res) {
console.log('onFileUploadStart');
console.log(file);
},
onFileUploadData: function (file, data, req, res) {
console.log('onFileUploadData');
console.log(file);
},
onFileUploadComplete: function (file, req, res) {
console.log('onFileUploadComplete');
console.log(file);
},
limits: {
fieldNameSize: 999999999,
fieldSize: 999999999
},
onParseStart: function() {
console.log('Starting to parse request!');
},
onParseEnd: function(req, next) {
console.log('Done parsing!');
next();
},
onError: function(e, next) {
if (e) {
console.log(e.stack);
}
next();
}
}));
And route: (I received file from client)
router.post('/newapi/addimages',function(req,res){
try{
// fileName = file.name, file name passed by client. Not used here. We use the name auto-generated by Node
var file = req.files.file;
var filePath = file.path;
var lastIndex = filePath.lastIndexOf("/");
var tmpFileName = filePath.substr(lastIndex + 1);
var image = req.body;
image.fileName = tmpFileName;
return res.send({ok:'heyyou'});
}
catch(err){
console.log(err);
return res.send({ok:'heyyou'});
}
});
My result:
Upvotes: 0
Views: 907
Reputation: 5265
It's because you have the inMemory
option set to true
. This option makes the image stay in the process memory instead of piping it to the filesystem and is accessible through the req.files.file.buffer
.
Change inMemory
to false
and you'll be good to go.
You don't have to set each option, and for your application you'd be good with most default values.
app.use(multer({ dest: path.join(__dirname, 'uploads') }));
Upvotes: 4