最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

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

Share Improve this question asked May 23, 2017 at 23:02 AkashiiAkashii 2,2815 gold badges18 silver badges31 bronze badges 1
  • "cross-device link not permitted" - this is outside the realm of Node/JavaScript and is coming from the filesystem. Check the actual paths used to verify they are as expected and verify on what partition(s) they live. – user2864740 Commented May 24, 2017 at 0:00
Add a comment  | 

3 Answers 3

Reset to default 10

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/";

发布评论

评论列表(0)

  1. 暂无评论