I've been playing with some code I found in a book about Node.js. It is a simple app which uploads images.
It shows the EXDEV error (500 Error: EXDEV, rename).
Could someone give me a hint? Here's my code:
exports.submit = function(dir) {
return function(req, res, next) {
var img = req.files.photo.image;
var name = req.body.photo.name || img.name;
var path = join(dir, img.name);
fs.rename(img.path, path, function (err) {
if(err) return next(err);
Photo.create({
name: name,
path: img.name
}, function (err) {
if(err) return next(err);
res.redirect('/');
});
});
};
};
I've been playing with some code I found in a book about Node.js. It is a simple app which uploads images.
It shows the EXDEV error (500 Error: EXDEV, rename).
Could someone give me a hint? Here's my code:
exports.submit = function(dir) {
return function(req, res, next) {
var img = req.files.photo.image;
var name = req.body.photo.name || img.name;
var path = join(dir, img.name);
fs.rename(img.path, path, function (err) {
if(err) return next(err);
Photo.create({
name: name,
path: img.name
}, function (err) {
if(err) return next(err);
res.redirect('/');
});
});
};
};
Share
Improve this question
edited Dec 4, 2014 at 11:56
Qantas 94 Heavy
16k31 gold badges72 silver badges88 bronze badges
asked Jan 12, 2014 at 4:25
user2837851user2837851
1,1913 gold badges15 silver badges20 bronze badges
0
2 Answers
Reset to default 21Renaming files cannot be done cross-device. My guess is that your upload directory (which by default is /tmp
) is on another partition/drive as your target directory (contained in the dir
variable).
Some solutions:
- configure the upload directory to be on the same partition/drive as your target directory; this depends on which module you're using to handle file uploads,
express.bodyParser
(and the module it uses,connect.multipart
) accepts anuploadDir
option that you can use; before starting your Node app, set the
TMPDIR
environment variable to point to a temporary directory on the same partition/drive as your target directory. If you're using a Unix-type OS:env TMPDIR=/path/to/directory node app.js
instead of setting the environment variable from your shell, set it at the top of your Node app:
process.env.TMPDIR = '/path/to/directory';
- instead of renaming, use a module like
mv
that can work cross-device;
Using Windows XP, I added to app.js:
process.env.TMPDIR = '.'; //new