In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.