Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of calling fs.renameSync error by Node.js

Shulou Source: shulou.com Published: 2022-06-01 01:56:35 09月13日 Update

This article mainly explains "how to solve the error when Node.js calls fs.renameSync". The explanation in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn together "how to solve the error when Node.js calls fs.renameSync"!

Error calling fs.renameSync method while writing a file upload function

The error code is as follows:

function upload(response,request){ console.log("upload called"); var form = new formidable.IncomingForm(); 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:"); response.write("

"); response.end(); }); }

After a rough analysis, it is expected that there will be permission problems when moving or manipulating files across disk partitions.

Here are two solutions:

Method 1:

CreateReadStream, CreateWriteSdream, and unlinkSync methods that primarily utilize fs

The specific codes are as follows:

function upload(response,request){ console.log("upload called"); var form = new formidable.IncomingForm(); console.log("about to parse"); form.parse(request, function(error, fields, files) { console.log("parsing done"); // fs.renameSync(files.upload.path, "./ tmp/test.jpg"); 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); }); response.writeHead(200, {"Content-Type": "text/html"}); response.write("received image:"); response.write("

"); response.end(); }); }

PS: I use node version 0.10.69, if you use version 0.6 or less, you can use util.pump

The corresponding code only needs to change the readStream.on position in the above code to: (Note the introduction of util module)

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

Method 2:

This one is much simpler.

Add a form.uploadDir='tmp'(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(, {"Content-Type": "text/html"}); response.write("received image:"); response.write("

"); response.end(); }); } Thank you for reading, the above is" Node.js call fs.renameSync error how to solve "the content, after learning this article, I believe that everyone on Node.js call fs.renameSync error how to solve this problem has a deeper understanding, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Code method learning content file version problem brevity method function just that is idea situation location article time more permissions module Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple Shulou Information vpn NVidia