8

我在上传文件 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 上运行项目时,终端上会出现错误。

任何身体都无法帮助?该代码在基本操作系统和拱门上完美运行。

4

10 回答 10

17

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

        // Read the file
        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!');
            });
        });
于 2017-05-23T23:57:50.083 回答
6

您可以使用“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;
        }
    });
于 2019-09-19T06:14:22.983 回答
1

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

于 2017-11-17T17:22:53.773 回答
1
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); 
于 2018-10-18T11:33:21.490 回答
0

您必须使用 fs.writeFileSync 否则可以在复制之前删除旧文件

fs.writeFileSync(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!');
});
于 2018-10-12T08:31:37.700 回答
0

在我的情况下,它在同一磁盘(驱动器)路径中工作意味着在 c:/ 中,如果新路径磁盘驱动器与临时路径磁盘驱动器不同,则会发生错误。

于 2022-01-27T08:59:44.083 回答
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);

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

于 2017-05-19T09:33:05.853 回答
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);
于 2018-03-29T11:13:12.427 回答
0

当您的临时文件夹位于不同的驱动器且节点代码位于不同的驱动器时,会出现此问题。

它会抛出这个错误。

解决方案在另一个 StackOverflow 问题中。

“EXDEV:不允许跨设备链接”错误是什么意思?

这篇文章的解决方案:

根据新的NodeJS,解决方案是不要移动,复制它

fs.copyFileSync(oldpath, newpath);
fs.unlink(oldpath,()=>{});

我的 Windows 11 在 SSD 中,我的代码在 HDD 中。上传的图像将下载到 Windows 驱动器中,我正在尝试将其移动到 HDD。

于 2021-12-13T08:48:32.673 回答
-1

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

  1. 通过命令安装 mv

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

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

    mv('source_file', 'destination_file,' function(err) {
        if (err) { throw err; }
    console.log('file moved successfully');
    };
    
于 2016-07-20T07:26:45.730 回答