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

4

在stack overflow上有很多类似我的问题,但都没有解决我的问题。

我在Ubuntu 18.04上遇到了这个错误:

错误:EXDEV:不允许跨设备链接,重命名'/tmp/upload_df97d265c452c510805679f968bb4c17' -> '/home/haider/workspaceNode/DSC_0076.JPG'

我尝试了这段代码

 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;
      var newpath = '/home/haider/workspaceNode/' + files.filetoupload.name;
      fs.rename(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();
  }
}).listen(8081);
2个回答

6

我认为Node的fs.rename不能在不同的文件系统之间重命名(也就是说,只能在一个文件系统内进行链接/取消链接)。

无论你的/home在哪里,可以肯定的是/tmp是一个tmpfs文件系统,实际上驻留在内存中。(可以在mount命令的输出中检查。)

因此,要移动文件,在目标位置必须使用fs.copyFile将数据复制到目标位置,然后fs.unlink原始下载文件。


因为它解决了我的文件上传问题,所以我点了赞。 - Aaron John Sabu

0
您可以将临时文件上传到脚本文件系统中的设备中:
var form = new formidable.IncomingForm({
  uploadDir: __dirname + '/tmp',  // don't forget the __dirname here
  keepExtensions: true
});

从这里开始 https://dev59.com/8mzXa4cB1Zd3GeqPT3Zk#14061432

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接