postgresql 错误:EXDEV:不允许跨设备链接,在 Ubuntu 16.04 LTS 上重命名“/tmp/”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37153666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: EXDEV: cross-device link not permitted, rename '/tmp/ on Ubuntu 16.04 LTS
提问by Faris Ridho
I have problem upload file nodejs. i have read stack1and stack2. but i think this different case on terminal output like this :
我在上传文件 nodejs 时遇到问题。我已经阅读了 stack1和 stack2。但我认为终端输出的这种不同情况是这样的:
> 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)
my code :
我的代码:
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;
}
code work on another distro like elementary os, but when i run project on debian jessie or ubuntu 16 LTS that error result on terminal.
代码在另一个发行版(如基本操作系统)上运行,但是当我在 debian jessie 或 ubuntu 16 LTS 上运行项目时,该错误会在终端上产生。
any body can't help ? that code just run perfectly on elementary os and arch.
任何身体都不能帮助?该代码在基本操作系统和架构上完美运行。
回答by framontb
Same problem. Workaround: Read the temp file, write the file in new location and remove the temp file:
同样的问题。解决方法:读取临时文件,将文件写入新位置并删除临时文件:
// 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!');
});
});
回答by VnDevil
You can use 'mv' packgage to resolve this issue Link NPM: https://www.npmjs.com/package/mvHow to use:
你可以使用'mv'包来解决这个问题链接NPM:https://www.npmjs.com/package/mv 使用方法:
Replace some old code with fs.rename:
用 fs.rename 替换一些旧代码:
fs.rename(temp_path, target_path, (err) => {
if (err) {
console.log('> FileServer.jsx | route: "/files/upload" | err:', err);
throw err;
}
});
With:
和:
const mv = require('mv');
mv(temp_path, target_path, function (err) {
if (err) {
console.log('> FileServer.jsx | route: "/files/upload" | err:', err);
throw err;
}
});
回答by Daggie Blanqx - Douglas Mwangi
it assisted me when working with nodejs , the concept should be the same https://stackoverflow.com/a/43206506/7878987
它在使用 nodejs 时帮助了我,概念应该是相同的https://stackoverflow.com/a/43206506/7878987
回答by manoj yadav
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);
回答by Павел Нохрин
You have to use fs.writeFileSync otherwise oldfile can be deleted before copied
你必须使用 fs.writeFileSync 否则 oldfile 可以在复制之前被删除
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!');
});
回答by Vijay Kumar
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);
回答by nityanarayan44
In order to resolve this problem, you can rewrite as:
为了解决这个问题,你可以重写为:
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);
and for multiple files, you could iterate this block based on length of files.
对于多个文件,您可以根据文件长度迭代此块。
回答by Dinesh Kumar
yes, you can resole this problem as.
是的,你可以解决这个问题。
install mv by command
npm install mv --save
add mv in your nodejs file
var mv = require('mv');<br>
move file as:
mv('source_file', 'destination_file,' function(err) { if (err) { throw err; } console.log('file moved successfully'); };
通过命令安装 mv
npm install mv --save
在你的 nodejs 文件中添加 mv
var mv = require('mv');<br>
将文件移动为:
mv('source_file', 'destination_file,' function(err) { if (err) { throw err; } console.log('file moved successfully'); };