CNode

使用formidable处理图片上传时,new formidable.IncomingForm().parse()回调里的files对象为空

问答
Wwalle-发布于11 年前最后回复11 年前4 回复8312 浏览0 收藏

其他的代码没贴,/index 是个表单,用来上传图像,提交到 /upload, 提交之后,form.parse(request, function (error, fields, files){}); 回调里面的files对象是个空对象,这是nodejs 入门里面的一个例子。刚开始学,请大家帮忙看看,不甚感激

var http = require('http');
var url  = require('url');
var formidable  = require('formidable');
var sys = require('sys');
var server = http.createServer(function (req,res){
	var pathname = url.parse(req.url).pathname;
	//console.log(req.method);
	if(req.url === '/upload' && req.method.toLowerCase() === 'post'){

		var form = new formidable.IncomingForm();
		form.parse(req, function (error, fields,files){
			console.log(files);  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!这里是一个空对象???
			res.writeHead(200,{'Content-Type': 'text/plain'});
			res.end(sys.inspect({fields: fields, files: files}));
		});
		return;
	};

	res.writeHead(200,{'Content-Type': 'text/html'});
	res.end('<!DOCTYPE html>'+
			'<html lang="en">'+
			'<head>'+
				'<meta charset="UTF-8">'+
				'<title>nodeJs</title>'+
			'</head>'+
			'<body>'+
				'<form action="/upload" method="post" entype="multipart/form-data">'+
					'<table>'+
						'<tr>'+
							'<td>'+
								'<input type="file" name="upload" multiple="multiple">'+
							'</td>'+
						'</tr>'+
						'<tr>'+
							'<td>'+
								'<input type="submit" value="submit">'+
							'</td>'+
						'</tr>'+
					'</table>'+
			'</body>'+
			'</html>');
});
server.listen('8080');
console.log('server start');
查看回复

回复 (4)

D
DevinXian#1·11 年前

你先在浏览器看看post报文请求体是不是正确

W
walle-#2·11 年前
引用 DevinXian你先在浏览器看看post报文请求体是不是正确

@DevinXian 谢谢啊,不过错误原因是我疏忽把 enctype 写成了 entype,抱歉浪费你时间了

D
DevinXian#3·11 年前
引用 walle-@DevinXian 谢谢啊,不过错误原因是我疏忽把 enctype 写成了 entype,抱歉浪费你时间了

@walle- - -恭喜找到小虫子这种小错误一般比较坑,因为比较隐蔽

W
walle-#4·11 年前
引用 DevinXian@walle 恭喜找到小虫子 这种小错误一般比较坑,因为比较隐蔽

@DevinXian 哈哈,是的,我昨天调试了好久,怎么也想不到是单词打错了

参与回复
登录后即可参与回复。登录