代码: var formidable=require(“formidable”);
function upload(response,request) { console.log(“Request handler ‘upload’ was called.”); //已输出
var form = new formidable.IncomingForm(); //日志显示,此行报错
console.log(“about to parse”);
form.uploadDir="./tmp";
form.parse(request, function(error, fields, files) {
console.log(“parsing done”);
fs.renameSync(files.upload.path, “./tmp/test2.png”);
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:
");
response.write("");
response.end();
});
}
错误日志: Request handler ‘upload’ was called.
D:\node_modules\formidable\lib\incoming_form.js:28 this.uploadDir = opts.uploadDir || os.tmpDir(); ^ TypeError: Object #<Object> has no method ‘tmpDir’ at new IncomingForm (D:\node_modules\formidable\lib\incoming_form.js:28:41) at Object.upload [as /upload] (D:\requestHandlers.js:29:14) at route (D:\route.js:4:25) at Server.onRequest (D:\server.js:8:3) at Server.emit (events.js:70:17) at HTTPParser.onIncoming (http.js:1479:12) at HTTPParser.onHeadersComplete (http.js:102:31) at Socket.ondata (http.js:1375:22) at TCP.onread (net.js:334:27)
而且formidable模块也已经成功安装 我使用npm list查看,输入结果:
D:>npm list
D:
├── formidable@1.0.14
错误是什么原因引发的呢?
这个错误说的挺清楚的了 this.uploadDir = opts.uploadDir || os.tmpDir(); opts.uploadDir是undefined os这个对象里面没有temDir方法。所以报错了。 解决方法就是自己设定一个上传的临时目录。 例如 require(“formidable”).IncomingForm.UPLOAD_DIR = configuration.config.uploadDir;//全局设定的 form.uploadDir=configuration.config.uploadDir;//只是对某个IncomingForm实例的uploadDor赋值 补充:我的formidable模块的版本是 1.0.11
http://www.nodebeginner.org/index-zh-cn.html
早上看这里的时候也是遇到这个问题,
@mmchjl 的全局设定方法没用过,针对某个实例设置那个我试了下也不行,
最后是改成
var forma = new formidable.IncomingForm({uploadDir:"/tmp"});