最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: EXDEV: cross-device link not permitted, rename - Stack Overflow

programmeradmin1浏览0评论

There are many question similar to my question on stack overflow. However not solved my problem.

I am getting this error on Ubuntu 18.04:

Error: EXDEV: cross-device link not permitted, rename '/tmp/upload_df97d265c452c510805679f968bb4c17' -> '/home/haider/workspaceNode/DSC_0076.JPG'

I Tried This code

 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.IningForm();
    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);

There are many question similar to my question on stack overflow. However not solved my problem.

I am getting this error on Ubuntu 18.04:

Error: EXDEV: cross-device link not permitted, rename '/tmp/upload_df97d265c452c510805679f968bb4c17' -> '/home/haider/workspaceNode/DSC_0076.JPG'

I Tried This code

 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.IningForm();
    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);
Share asked Dec 3, 2019 at 4:52 Haider YaqoobHaider Yaqoob 1,9332 gold badges12 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

I suppose that Node's fs.rename cannot rename across filesystems (that is, limited to link/unlink within one filesystem).

Wherever your /home is, it's a safe bet to suppose that /tmp is a tmpfs filesystem actually residing in memory. (You can check in the output of mount.)

So, to move a file, you have to fs.copyFile your data to the destination, then fs.unlink the original downloaded file.

You can upload a temporary file into a device with script's file system:

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

from here https://stackoverflow./a/14061432/7773566

发布评论

评论列表(0)

  1. 暂无评论