Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: EXDEV: cross-device link not permitted, rename nodejs

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

like image 739
Akashii Avatar asked May 23 '17 23:05

Akashii


3 Answers

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) {
....
....
});
like image 191
Narmada Maligireddy Avatar answered Nov 12 '22 04:11

Narmada Maligireddy


problem is with rename method. use 'mv' package to move your file

https://www.npmjs.com/package/mv

like image 24
Hemant Tripathi Avatar answered Nov 12 '22 04:11

Hemant Tripathi


you can just add the following code to solve this problem: var form = new formidable.IncomingForm(); form.uploadDir="yourDirNameHere/";

like image 1
Changyuan Chen Avatar answered Nov 12 '22 02:11

Changyuan Chen