function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files) {
console.log("parsing done");
/*may error on Windows system:
rename to an already existing file */
console.log(files.upload.path); //print file path in console
fs.rename(files.upload.path, 'C:/Users/HIEUXI~1/AppData/Local/Temp/test.png', function(err) {
if (err) {
fs.unlink('C:/Users/HIEUXI~1/AppData/Local/Temp/test.png');
console.log("sucessfully deleted /tmp/test.png");
fs.rename(files.upload.path,'C:/Users/HIEUXI~1/AppData/Local/Temp/test.png');
console.log("Renamed complete");
}
});
response.writeHead(200, {"Content-Type": "text/html"});
response.write("Recevied the image<br/>");
response.write("<img src='/show' />");
console.log("Received complete")
response.end();
});
}
Trong cái đoạn code này em dùng module fs để rename file’s path mà lúc em gõ C:…\temp\test.png thì nó bị lỗi ENOENT lúc unlink lên stackoverflow nó chỉ đổi \ thành / thì unlink được. Hơi khó hiểu, ai chỉ cho em vì sao đổi \ thành / thì chạy được thế, trong khi em console.log(old file path) thì nó là C:…\temp… mà