The Node Beginner Book

书中的实例代码当上传图片时会报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

有误之处还请不吝赐教!

最新文章

  1. Integer.parseInt 引发的血案
  2. 转: 深入理解Linux修改hostname
  3. gong server
  4. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials
  5. PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone
  6. IOS中如何获取手机的当前IP
  7. Delphi XE5教程5:程序的结构和语法
  8. 1201.1——Vim编辑器的相关操作
  9. c#鼠标在控件上面,然后显示文字
  10. 使用Perl提取Excel中的IO_MUX
  11. 201621123057 《Java程序设计》第6周学习总结
  12. ●BOZJ 4456 [Zjoi2016]旅行者
  13. k8s 1.12.6版的kubeadm默认配置文件
  14. 12 week blog
  15. hibernate12--缓存
  16. ubuntu14.04 LTS 更新国内网易163源
  17. Spark之standalone模式
  18. LeetCode OJ 24. Swap Nodes in Pairs
  19. .NET中的FileUpload控件的使用-Jquery(一)
  20. Mybatis-Dao层开发之Mapper接口

热门文章

  1. Nacicat for Oracle 绿色版 亲测可用
  2. JAVA面向对象思想理解分析
  3. LeetCode 中级 - 翻转矩阵后的得分(861)
  4. 【TOJ 3812】Find the Lost Sock(异或)
  5. BZOJ1432: [ZJOI2009]Function(找规律)
  6. mysql 排名
  7. HTTP学习之HTTP基础
  8. shell重温---基础篇(输入/输出重定向)
  9. HBase java API 的使用范例(增,删,查,扫描)
  10. Git使用之一:创建仓储和提交文件