在ExpressJS / NodeJS中移动文件 [英] Move File in ExpressJS/NodeJS

查看:245
本文介绍了在ExpressJS / NodeJS中移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NodeJS / ExpressJS将上传的文件从 / tmp 移动到 home 目录: p>

I'm trying to move uploaded file from /tmp to home directory using NodeJS/ExpressJS:

fs.rename('/tmp/xxxxx', '/home/user/xxxxx', function(err){
    if (err) res.json(err);

console.log('done renaming');
});

但它没有工作,没有遇到任何错误。但是,当新路径也在 / tmp 中时,这将起作用。

But it didn't work and no error encountered. But when new path is also in /tmp, that will work.

我使用Ubuntu, home 在不同的分区。任何修复?

Im using Ubuntu, home is in different partition. Any fix?

谢谢

推荐答案

是的,fs.rename不会在两个不同的磁盘/分区之间移动文件。这是正确的行为。 fs.rename 提供与linux中 rename(2)相同的功能。

Yes, fs.rename does not move file between two different disks/partitions. This is the correct behaviour. fs.rename provides identical functionality to rename(2) in linux.

阅读相关问题此处

要获得所需的内容,您必须执行以下操作:

To get what you want, you would have to do something like this:

var source = fs.createReadStream('/path/to/source');
var dest = fs.createWriteStream('/path/to/dest');

source.pipe(dest);
source.on('end', function() { /* copied */ });
source.on('error', function(err) { /* error */ });

这篇关于在ExpressJS / NodeJS中移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆