Nodejs prompt: cross device link not allowed, rename error solution

Time:2020-1-17

Node.js prompt error:

Error: EXDEV: cross-device link not permitted, rename ‘C:\Users\THEDIS~1\AppData\Local\Temp\upload_9b46f1afc2f9ade074037c3fb707d271’ -> ‘E:/node-rumen/tmp/test.png’

Error calling fs.renamesync method when uploading files

This prompt is about the permission problem of cross region rename file.

Resolvent

First copy from the source file to the target file of another partition, and then unlink.


form.parse(request, function(error, fields, files) {
 console.log(“parsing done”);
 fs.renameSync(files.upload.path, “/tmp/test.png”);
});

Change to


form.parse(request, function(error, fields, files) {
 console.log(“parsing done”);

 // fs.renameSync(files.upload.path, “/nodejs/case/two/img/test.png”);

 var readStream=fs.createReadStream(files.upload.path);
 var writeStream=fs.createWriteStream("./tmp/test.jpg");
 readStream.pipe(writeStream);
 readStream.on('end',function(){
  fs.unlinkSync(files.upload.path);
 });
});

PS: if the node version is 0.10.69, the above method can be used. If the node version is less than 0.6, util.pump can be used

The corresponding code only needs to change the readstream.on in the above code to: (note the introduction of util module)


util.pump(readStream,writeStream, function() {
 fs.unlinkSync('files.upload.path');
});

Reference address: https://stackoverflow.com/que

https://stackoverflow.com/que…

Method two:

It’s a lot simpler

Add oneform.uploadDir='tmp' OK (write a temporary path)


function upload(response,request){
 console.log(“upload called”);
 var form = new formidable.IncomingForm();
 form.uploadDir='tmp';

 console.log(“about to parse”);
 form.parse(request, function(error, fields, files) {
  console.log(“parsing done”);
  fs.renameSync(files.upload.path, “./tmp/test.jpg”);
  response.writeHead(200, {“Content-Type”: “text/html”});
  response.write(“received image:<br/>”);
  response.write(“<img src='/show' />”);
  response.end();
 });
}

summary

The above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. Thank you for your support for developepaer.

Recommended Today

See through the analysis and practice of spring MVC source code — basic knowledge of website

Website architecture and its evolution Infrastructure Network transmission decomposition mode: Standard OSI reference model TCP / IP reference model Solutions for massive data Caching and page static cache Stored directly in memory through program Using cache framework (encache, redis, Memcache) Page static Build using template technology (velocity, freemaker, etc.) Database optimization Table structure optimization SQL […]