在stack overflow上有很多类似我的问题,但都没有解决我的问题。
我在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);