nodejs上传图片Error: ENOENT, no such file
在学习node.js的时候,学习上传图片并显示到网页上,上传过程中出现下面错误:
{ [Error: ENOENT, no such file or directory 'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\
upload_5f4be95217f941dd8059acf89eff05d1']
errno: -4058,
code: 'ENOENT',
path: 'C:\\DOCUME~1\\ADMINI~1\\LOCALS~1\\Temp\\upload_5f4be95217f941dd8059acf8
9eff05d1',
syscall: 'rename' }
网上查询资料,说是fs的rename 不允许跨 分区移动文件。
操作方法
- 01
安装node.js ,安装成功后,安装formidable(npm install formidable)。 我的nodejs安装目录(默认)是:C:\Program Files\nodejs
- 02
工作目录放在D盘中,目录是D:\node-test,在D:\node-test下安装的formidable.
- 03
上传文件方法 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"); console.log("F.U.P: " + files.upload.path); try{ fs.renameSync(files.upload.path, "/tmp/test.png"); //对应目录d:/tmp/test.png }catch(e){ console.log(e); } response.writeHead(200,{"Content-Type":"text/html"}); response.write("received image:<br/>"); response.write("<img src='/show' />"); response.end(); }); }
- 04
上传后控制台报错 { [Error: ENOENT, no such file or directory 'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ upload_5f4be95217f941dd8059acf89eff05d1'] errno: -4058, code: 'ENOENT', path: 'C:\\DOCUME~1\\ADMINI~1\\LOCALS~1\\Temp\\upload_5f4be95217f941dd8059acf8 9eff05d1', syscall: 'rename' }
- 05
我的nodejs安装目录是c盘下,现在工作目录在d盘下,应该是这个的问题。 修改为fs.renameSync(files.upload.path, "c:/tmp/test.png"); 后上传成功。