错误:EXDEV:不允许跨设备链接,重命名nodejs

时间:2017-05-23 23:02:52

标签: javascript node.js

我使用强大的上传文件,但是我收到此错误

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

这是我的代码:

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) {
    });
})

当我删除function checkFile并更改为form.uploadDir = path.join(__dirname, '../public/uploads');时,它会完美无缺。我哪里错了?请帮帮我

3 个答案:

答案 0 :(得分:4)

问题在于重命名方法。 使用' mv'包移动你的文件

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

答案 1 :(得分:1)

谢谢。使用“ mv”包而不是文件系统“重命名”方法来解决在文件上传期间将文件移动到另一个文件夹时发生的错误:

  

“错误:EXDEV:不允许跨设备链接,请重命名...”

使用cmd安装了软件包“ mv”:

npm install mv

用法:

var mv = require('mv');

mv('source/file', 'dest/file', function(err) {
....
....
});

答案 2 :(得分:0)

您只需添加以下代码即可解决此问题: var form = new formidable.IncomingForm(); form.uploadDir = __ dirname;