Reputation: 2839
I'm trying to save files with multer but it doesn't really want to work :
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './')
},
filename: function (req, file, cb) {
cb(null, file.originalname + '-' + Date.now() + '.' + path.extname(file.originalname));
}
});
var upload = multer({ storage: storage,
onFileUploadComplete : function (file) {
console.log('Completed file!');
},
onParseStart : function() {
console.log('whatevs');
}});
app.post('/upload' ,upload.single('thisisme'), function(req,res) {});
The file does get saved, but ParseStart or UploadComplete are never fired. Why is that? I also tried using the app.use ( multer ... );
Upvotes: 2
Views: 794
Reputation: 7204
It is because You are trying to use old multer api. In current version there is no event handlers: onFileUploadComplete
and onParseStart
. Please check documentation for api details : https://github.com/expressjs/multer
this part of Your code looks ok:
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './')
},
filename: function (req, file, cb) {
cb(null, file.originalname + '-' + Date.now() + '.' + path.extname(file.originalname));
}
});
and this is also ok:
app.post('/upload' ,upload.single('thisisme'), function(req,res) {});
this is wrong:
var upload = multer({ storage: storage,
onFileUploadComplete : function (file) {
console.log('Completed file!');
},
onParseStart : function() {
console.log('whatevs');
}});
change it to this:
var upload = multer({
storage: storage,
fileFilter:function(req, file, cb) {
//Set this to a function to control which files should be uploaded and which should be skipped. It is instead of onParseStart.
}
});
There is nothing instead of onFileUploadComplete
. But:
app.post('/upload' ,upload.single('thisisme'), function(req,res) {
//this is call when upload success or failed
});
you can change It to this:
app.post('/upload', function (req, res) {
upload.single('thisisme')(req, res, function (err) {
if (err) {
// An error occurred when uploading
return
}
// Everything went fine, and this is similar to onFileUploadComplete
})
})
Upvotes: 5