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

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

最佳答案

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

无论您在/home的何处,都可以假定/tmp是实际上驻留在内存中的tmpfs文件系统,这是一个安全的选择。 (您可以检入mount的输出。)

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

关于javascript - 错误:EXDEV:不允许跨设备链接,请重命名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59150353/

10-12 00:12
查看更多