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
JavaScript
x
26
26
1
var http = require('http');
2
var formidable = require('formidable');
3
var fs = require('fs');
4
5
http.createServer(function (req, res) {
6
if (req.url == '/fileupload') {
7
var form = new formidable.IncomingForm();
8
form.parse(req, function (err, fields, files) {
9
var oldpath = files.filetoupload.path;
10
var newpath = '/home/haider/workspaceNode/' + files.filetoupload.name;
11
fs.rename(oldpath, newpath, function (err) {
12
if (err) throw err;
13
res.write('File uploaded and moved!');
14
res.end();
15
});
16
});
17
} else {
18
res.writeHead(200, {'Content-Type': 'text/html'});
19
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
20
res.write('<input type="file" name="filetoupload"><br>');
21
res.write('<input type="submit">');
22
res.write('</form>');
23
return res.end();
24
}
25
}).listen(8081);
26
Advertisement
Answer
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.