【解决】Node JS Error: ENOENT
2024-08-24 19:43:00
书中的实例代码当上传图片时会报Error: ENOENT, 原因:图片默认会选择系统的缓存文件夹下,在windows下无权访问C盘,所以就报错了。。
解决方法:只需要指定图片存放位置,在这之前要在指定的文件下手工创建tmp文件夹;
具体改动如下(红色标注为改动内容):
upload.js
var form = new formidable.IncomingForm();
form.uploadDir = "tmp";
form.parse(req, function(err, fields, files) { console.log(files.upload.path);
try{
7 fs.renameSync(files.upload.path, "tmp/test.png");
8 }catch(e){
9 console.log(e);
10 } res.writeHead(, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(sys.inspect({fields: fields, files: files}));
});
当然,文件开头不要忘记引入fs模块 fs = require('fs')
requesthandle.js
function show(response, postData) {
console.log("Request handler 'show' was called.");
fs.readFile("tmp/test.png", "binary", function(error, file) {
if(error) {
response.writeHead(, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
} else {
response.writeHead(, {"Content-Type": "image/png"});
response.write(file, "binary");
response.end();
}
});
}
本人也刚刚开始研究nodejs,昨晚看了下这本书,上传搞了好久,在网上也google好久,仍不得其解,写出来以备忘,与大家共享,希望共同进步!
git地址:https://github.com/finderL/myapp
有误之处还请不吝赐教!
最新文章
- Integer.parseInt 引发的血案
- 转: 深入理解Linux修改hostname
- gong server
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials
- PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone
- IOS中如何获取手机的当前IP
- Delphi XE5教程5:程序的结构和语法
- 1201.1——Vim编辑器的相关操作
- c#鼠标在控件上面,然后显示文字
- 使用Perl提取Excel中的IO_MUX
- 201621123057 《Java程序设计》第6周学习总结
- ●BOZJ 4456 [Zjoi2016]旅行者
- k8s 1.12.6版的kubeadm默认配置文件
- 12 week blog
- hibernate12--缓存
- ubuntu14.04 LTS 更新国内网易163源
- Spark之standalone模式
- LeetCode OJ 24. Swap Nodes in Pairs
- .NET中的FileUpload控件的使用-Jquery(一)
- Mybatis-Dao层开发之Mapper接口
热门文章
- Nacicat for Oracle 绿色版 亲测可用
- JAVA面向对象思想理解分析
- LeetCode 中级 - 翻转矩阵后的得分(861)
- 【TOJ 3812】Find the Lost Sock(异或)
- BZOJ1432: [ZJOI2009]Function(找规律)
- mysql 排名
- HTTP学习之HTTP基础
- shell重温---基础篇(输入/输出重定向)
- HBase java API 的使用范例(增,删,查,扫描)
- Git使用之一:创建仓储和提交文件