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("parse done");
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, {"Content-type": "text/html"});
response.write("Received image:<br>");
response.write("<image src=/show />");
response.end();
});
}
上面是代码,下面是报错的信息。我其实就是在按照《nodeJs入门》那本书在做练习,不知道为啥代码敲完之后,它就报这个错。
fs.renameSync(files.upload.path, "/tmp/test.png");
^
TypeError: Cannot read property 'path' of undefined
at /Users/chenxiaochun/Documents/我的资料/test/nodeJs/requestHandlers.js:36:29
at IncomingForm.<anonymous> (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/node_modules/formidable/lib/incoming_form.js:118:9)
at IncomingForm.emit (events.js:67:17)
at IncomingForm._error (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/node_modules/formidable/lib/incoming_form.js:248:8)
at IncomingForm.write (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/node_modules/formidable/lib/incoming_form.js:145:10)
at IncomingMessage.<anonymous> (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/node_modules/formidable/lib/incoming_form.js:95:12)
at IncomingMessage.emit (events.js:67:17)
at HTTPParser.onBody (http.js:113:42)
at Socket.ondata (http.js:1410:22)
at TCP.onread (net.js:374:27)
我把完整的代码贴出来吧,分为以下几个部分。 index.js
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route, handle);
server.js
var http = require("http");
var url = require("url");
function start(route, handle){
function onRequest(request, response){
var postData = "";
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
request.setEncoding("utf8");
route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
route.js
function route(handle, pathname, response, request){
console.log("About route a request for " + pathname);
if(typeof handle[pathname] === "function"){
return handle[pathname](response, request);
}else{
console.log("No request handle for " + pathname +".");
response.writeHead("404", {"Content-type": "text/plain"});
response.write("404 not found.");
response.end();
}
}
exports.route = route;
requestHandler.js
var querystring = require("querystring");
var fs = require("fs");
var formidable = require("formidable");
function start(response){
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-type": "text/html"});
response.write(body);
response.end();
}
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("parse done");
console.log(files);
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, {"Content-type": "text/html"});
response.write("Received image:<br>");
response.write("<image src='/show' />");
response.end();
});
}
function show(response, postData){
console.log("Request handler 'show' was called.");
fs.readFile("/tmp/test.png", "binary", function(error, file){
if(error){
response.writeHead(500, {"Content-type": "text/plain"});
response.write(error + "\n");
response.end();
}else{
response.writeHead(200, {"Content-type": "text/plain"});
response.write(file, "binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
function start(response){
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-type": "text/html"});
response.write(body);
response.end();
}
您说的是这里吗,应该没问题啊。
这是加上您说的request.files.upload之后的upload方法:
function upload(response, request){
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("About to parse");
**console.log(request.files.upload);**
form.parse(request, function(error, fields, files){
console.log("parse done");
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, {"Content-type": "text/html"});
response.write("Received image:<br>");
response.write("<image src='/show' />");
response.end();
});
}
可是,运行之后找不到upload属性,下面是报错信息。
console.log(request.files.upload);
^
TypeError: Cannot read property 'upload' of undefined
at Object.upload [as /upload] (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/requestHandlers.js:35:27)
at route (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/router.js:5:25)
at Server.onRequest (/Users/chenxiaochun/Documents/我的资料/test/nodeJs/server.js:13:3)
at Server.emit (events.js:70:17)
at HTTPParser.onIncoming (http.js:1514:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1410:22)
at TCP.onread (net.js:374:27)
我是一个初学者,还不会使用您提到的那两个框架。
这是测试代码:
var formidable = require('formidable');
var http = require('http');
var util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.uploadDir = '/home/tmp';//手动设置默认上传tmp目录,可以通过fs.rename更改
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8080);
然后我上传一个文件后的打印信息是:
received upload:
{ fields: { title: '' },
files:
{ upload:
{ size: 4037,
path: '/home/tmp/0af01b60359775ed9e02c9384cc93dcc',
name: 'status.xsd',
type: 'application/octet-stream',
hash: false,
lastModifiedDate: Thu Aug 09 2012 13:46:21 GMT+0800 (CST),
_writeStream: [Object],
length: [Getter],
filename: [Getter],
mime: [Getter] } } }
想要的信息都有了,你可以通过files.upload取得所有你想要的信息
- 非常感谢您能抽时间给我写这个实例,不过,类似您的这个实例我也已经实现了。
- 我现在是初学者,不仅仅是想实现这个文件上传的功能,其实我更想知道:我上面自己所写的代码,到底是哪里不对,为什么不对。
- 再一次感谢您的帮助。
好吧,用你写的方法,完整代码在此:
var formidable = require('formidable');
var http = require('http');
var util = require('util');
var fs = require('fs');
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("parse done");
fs.renameSync(files.upload.path, "/home/tmp/test.png");
console.log(files);
response.writeHead(200, {"Content-type": "text/html"});
response.write("Received image:<br>");
response.write("<image src=/show />");
response.end();
});
}
function start(response){
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-type": "text/html"});
response.write(body);
response.end();
}
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
upload(res, req);
/*
var form = new formidable.IncomingForm();
form.uploadDir = '/home/tmp';//手动设置默认上传tmp目录,可以通过fs.rename更改
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
*/
}
start(res);
/*
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
*/
}).listen(8080);
form.parse(request, function(error, fields, files){
.....
}
我发现,这个位置,办理出您写的例子files对象,可以看到以下这堆东西:
{ upload:
{ size: 63949,
path: '/tmp/1189fcfbfe44620782b17efb53d23c04',
name: '1.png',
type: 'image/png',
hash: false,
lastModifiedDate: Fri, 10 Aug 2012 02:48:08 GMT,
_writeStream:
{ path: '/tmp/1189fcfbfe44620782b17efb53d23c04',
fd: 9,
writable: false,
flags: 'w',
encoding: 'binary',
mode: 438,
bytesWritten: 63949,
busy: false,
_queue: [],
drainable: true },
length: [Getter],
filename: [Getter],
mime: [Getter] } }
而我写的例子中的files对象仅仅是一个空的{},因此就造成无法找到path。
-
仔细看一下,它代码是这样调用的:
upload(res, req);
因此不可能是这个错误; -
若真弄反了,出错信息应该是
Cannot read property 'files' of undefined
而不是Cannot read property 'path' of undefined
Request handler 'upload' was called.
About to parse
Request handler 'start' was called.
parse done
{ upload:
{ size: 71040,
path: '\\tmp\\d8f69e1c65b5db83acdb1a109603884f',
name: 'php.ini',
type: 'application/octet-stream',
lastModifiedDate: Fri Aug 10 2012 11:47:00 GMT+0800 (中国标准时间),
_writeStream:
{ path: '\\tmp\\d8f69e1c65b5db83acdb1a109603884f',
fd: 3,
writable: false,
flags: 'w',
encoding: 'binary',
mode: 438,
bytesWritten: 71040,
busy: false,
_queue: [],
_open: [Function],
drainable: true },
length: [Getter],
filename: [Getter],
mime: [Getter] } }
Request handler 'start' was called.
问题解决了:
-
在文件server.js中,你不应该用
request.setEncoding("utf8");
来手动设置字符编码,当你设置了这个之后,会导致formidable解析出错; -
在文件requestHandler.js中,
form.parse(request, function(error, fields, files){
这里没有判断是否有出错信息error
,因为若error不为null,则fileds和files就可能不能得到正确的值; -
这也解释了为什么 @sumory 给出的精简代码能正确运行,而你这一陀代码却会会出错。
刚才闲得蛋疼,上楼主所说的《Node入门》(http://www.nodebeginner.org/index-zh-cn.html )看了一下,人家的代码是这样子的:
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
本以为是《Node入门》误人子弟,原来那句request.setEncoding("utf8");
还是楼主自己加上去的,真是