I upload file with formidable , but I get this error
Error: EXDEV: cross-device link not permitted, rename
Here is my code :
router.post('/file',function(req,res) {
var form = new formidable.IncomingForm();
function checkFile(filename) {
if(filename.match(/\.(jpg|jpeg|png)$/i)){
return form.uploadDir = path.join(__dirname, '../public/uploads/img');
}else{
return form.uploadDir = path.join(__dirname, '../public/uploads');
}
}
form.multiples = true;
form.maxFieldsSize = 2 * 300 * 300;
// every time a file has been uploaded successfully,
// rename it to it's orignal name
form.on('file', function(field, file) {
var fileName = file.name;
var d = new Date();
var t = d.getTime();
var newName = md5(file.name) + t;
fs.rename(file.path,path.join(checkFile(fileName),newName),function(err) {
if(err)
console.log(err);
console.log('Success')
});
});
// log any errors that occur
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
// parse the incoming request containing the form data
form.parse(req, function(err, fields, files) {
});
})
When I remove function checkFile
and change to form.uploadDir = path.join(__dirname, '../public/uploads');
, it work perfect . Where is my wrong ? Please help me
Thanks. Used 'mv' package instead of filesystem 'rename' method to solve the error occurred in moving the file to another folder during file upload:
"Error: EXDEV: cross-device link not permitted, rename..."
Installed package 'mv' using cmd:
npm install mv
Usage:
var mv = require('mv');
mv('source/file', 'dest/file', function(err) {
....
....
});
problem is with rename method. use 'mv' package to move your file
https://www.npmjs.com/package/mv
you can just add the following code to solve this problem: var form = new formidable.IncomingForm(); form.uploadDir="yourDirNameHere/";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With