【问题标题】:Error: EXDEV: cross-device link not permitted, rename错误:EXDEV:不允许跨设备链接,重命名
【发布时间】:2019-12-03 04:52:15
【问题描述】:

有很多问题类似于我关于堆栈溢出的问题。但是没有解决我的问题。

我在 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);

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    我想 Node 的 fs.rename 不能跨文件系统重命名(也就是说,仅限于在一个文件系统内链接/取消链接)。

    无论您的/home 在哪里,都可以假设/tmp 是一个实际驻留在内存中的tmpfs 文件系统。 (可以查看mount的输出。)

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

    【讨论】:

    • 赞成,因为它也解决了我的文件上传问题
    【解决方案2】:

    您可以将临时文件上传到具有脚本文件系统的设备中:

    var form = new formidable.IncomingForm({
      uploadDir: __dirname + '/tmp',  // don't forget the __dirname here
      keepExtensions: true
    });
    

    从这里https://stackoverflow.com/a/14061432/7773566

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 2021-02-09
      • 2014-01-31
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 2018-07-12
      相关资源
      最近更新 更多