Search code examples
javascriptnode.jswindows-servicesmulternode-windows

multer file-upload doesn't work with node-windows


I am trying to upload picture files, using multer. This is is my code:

router.post('/', function (req, res) {
    upload(req, res, function (err) {
    if (err) {
  // An error occurred when uploading
  conole.log(err);
  return;
}

// Everything went fine
var data = Object.assign({picture: req.file && req.file.filename}, req.body);
db.create('devices', data)
.then(function(data){
    res.render('msg', {msg: 'Added device '+ data[0]});
})
.catch(function(err){
    console.log(err);
});

});

});

However, when I try to run the server from the command-line node app.js, everything is working as expected. But when I'm running it from A windows service, using node-windows, it doesn't seem to work, while I don't get any errors. (Meaning the picture file name is actually recorded in the database, but the file doesn't upload. is it maybe a destination issue? if so, I will provide my destination code:

var storage = multer.diskStorage({
destination: './public/uploads/',
filename: function (req, file, cb) {
  cb(null,  file.originalname + "-" + Date.now() + path.extname(file.originalname));
}
});

Solution

  • remove this line from your code

    destination: './public/uploads/',
    

    and add this code

    destination: __dirname+'../../../uploads',
    

    this will work both mac OS and windows