错误:EXDEV:不允许跨设备链接,重命名' / tmp / on Ubuntu 16.04 LTS

时间:2016-05-11 05:19:51

标签: node.js postgresql ubuntu-16.04 cross-device

我有问题上传文件nodejs。我读过了 stack1stack2。但我认为这种情况不同 在这样的终端输出上:

> Unhandled rejection Error: EXDEV: cross-device link not permitted,
> rename '/tmp/31941-53enly.png' ->
> 'public/files/complaint/Screenshot_2016-05-01_01-16-55.png'
>     at Error (native)
>     at Object.fs.renameSync (fs.js:681:18)
>     at null.<anonymous> (/home/faris/myprojects/orchestrale-server/routes/complaintimage.js:163:20)
>     at tryCatcher (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/util.js:16:23)
>     at Promise._settlePromiseFromHandler (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:502:31)
>     at Promise._settlePromise (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:559:18)
>     at Promise._settlePromise0 (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:604:10)
>     at Promise._settlePromises (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:683:18)
>     at Async._drainQueue (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/async.js:138:16)
>     at Async._drainQueues (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/async.js:148:10)
>     at Immediate.Async.drainQueues [as _onImmediate] (/home/faris/myprojects/orchestrale-server/node_modules/sequelize/node_modules/bluebird/js/release/async.js:17:14)
>     at processImmediate [as _immediateCallback] (timers.js:383:17)

我的代码:

  if (_.isEmpty(req.files) == false) {
      var tp = avatar_folder+req.files.file.name;
      fs.renameSync(req.files.file.path, tp, function(err){});
      var avatar = req.files.file.name;
      }

代码工作在另一个发行版像基本操作系统,但当我在debian jessie或ubuntu 16 LTS上运行项目时,错误导致终端。

任何人都无法帮助?该代码只是在基本的os和arch上完美运行。

8 个答案:

答案 0 :(得分:8)

同样的问题。解决方法:读取临时文件,将文件写入新位置并删除临时文件:

export

答案 1 :(得分:2)

您可以使用“ mv”包装来解决此问题 连结NPM:https://www.npmjs.com/package/mv 使用方法:

用fs.rename替换一些旧代码:

    fs.rename(temp_path, target_path, (err) => {
        if (err) {
            console.log('> FileServer.jsx | route: "/files/upload" | err:', err);
            throw err;
        }
    });

使用:

    const mv = require('mv');
    mv(temp_path, target_path, function (err) {
        if (err) {
            console.log('> FileServer.jsx | route: "/files/upload" | err:', err);
            throw err;
        }
    });

答案 2 :(得分:0)

要解决此问题,您可以重写为:

fs.writeFile(path_where_to_write, file, function(err) {
    if (err) throw err; /*do something else.*/
});
/*and then Remove the file from tmp location*/
fs.unlink(tempPath);

对于多个文件,您可以根据文件的长度迭代此块。

答案 3 :(得分:0)

它在使用nodejs时帮助了我,概念应该是相同的https://stackoverflow.com/a/43206506/7878987

答案 4 :(得分:0)

var http = require('http');
var formidable = require('formidable');
var fs = require('fs');

http.createServer(function (req, res) {
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.path;
console.log(oldpath);
var newpath = 'F:/your folder name/' + files.filetoupload.name;
fs.readFile(oldpath, function (err, data) {
if (err) throw err;
console.log('File read!');

// Write the file
fs.writeFile(newpath, data, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
console.log('File written!');
});

// Delete the file
fs.unlink(oldpath, function (err) {
if (err) throw err;
console.log('File deleted!');
});
});
});
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="fileupload" method="post" enctype="multipart/form- 
data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(8080);

答案 5 :(得分:0)

您必须使用fs.writeFileSync 否则,可以在复制之前删除oldfile

fs.writeFileSync(newpath,data,function(err){

if (err) throw err;
res.write('File uploaded and moved!');
res.end();
console.log('File written!');

});

//删除文件 fs.unlink(oldpath,function(err){

if (err) throw err;
console.log('File deleted!');

});

答案 6 :(得分:0)

var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
var mv = require('mv');

http.createServer(function (req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      var oldpath = files.filetoupload.path;
     console.log(oldpath);
      var newpath = 'F:/change path to your project dir/' + files.filetoupload.name;
      mv(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write('File uploaded and moved!');
        res.end();
      });
 });
  } else {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="filetoupload"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }enter code here
}).listen(8080); 

答案 7 :(得分:-2)

是的,你可以解决这个问题。

  1. 通过命令安装mv

    npm install mv --save
    
  2. 在您的nodejs文件中添加mv

    var mv = require('mv');<br>
    
  3. 将文件移动为:

    mv('source_file', 'destination_file,' function(err) {
        if (err) { throw err; }
    console.log('file moved successfully');
    };