AkshayP AkshayP - 2 months ago 18
Node.js Question

How to set different destinations in nodejs using multer?

I'm trying to upload any file using Multer package. It's working fine when I use following code in my

server.js
file.

var express = require('express'),
app = express(),
multer = require('multer');
app.configure(function () {
app.use(multer({
dest: './static/uploads/',
rename: function (fieldname, filename) {
return filename.replace(/\W+/g, '-').toLowerCase();
}
}));
app.use(express.static(__dirname + '/static'));
});

app.post('/api/upload', function (req, res) {
res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name});
});

var server = app.listen(3000, function () {
console.log('listening on port %d', server.address().port);
});


What I want is to store file at different locations. I had tried following code but it does not work for me.

var express = require('express'),
app = express(),
multer = require('multer');
app.configure(function () {
app.use(multer({
//dest: './static/uploads/',
rename: function (fieldname, filename) {
return filename.replace(/\W+/g, '-').toLowerCase();
}
}));
app.use(express.static(__dirname + '/static'));
});

app.post('/api/pdf', function (req, res) {
app.use(multer({ dest: './static/pdf/'}));
res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name});
});

app.post('/api/image', function (req, res) {
app.use(multer({ dest: './static/image/'}));
res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name});
});

app.post('/api/video', function (req, res) {
app.use(multer({ dest: './static/video/'}));
res.send({image: true, file: req.files.userFile.originalname, savedAs: req.files.userFile.name});
});

var server = app.listen(3000, function () {
console.log('listening on port %d', server.address().port);
});


Means, if I hit
http://localhost:3000/api/pdf
file should store at 'pdf' folder, if I hit
http://localhost:3000/api/video
file should store at 'video' folder.

Is there any way to achieve this aim?

Thank you in advance.

Answer

You can use changeDest.

Function to rename the directory in which to place uploaded files.

It is available from v0.1.8

app.post('/api/:type', multer({
dest: './uploads/',
changeDest: function(dest, req, res) {
    var newDestination = dest + req.params.type;
    var stat = null;
    try {
        stat = fs.statSync(newDestination);
    } catch (err) {
        fs.mkdirSync(newDestination);
    }
    if (stat && !stat.isDirectory()) {
        throw new Error('Directory cannot be created because an inode of a different type exists at "' + dest + '"');
    }
    return newDestination
}
}), function(req, res) {
     //set your response
});