关于express 4.2.0 上传图片
发布于 10 年前 作者 AntSworD 5491 次浏览 最后一次编辑是 8 年前
<form method="post" enctype="multipart/form-data" action="/set_avatar">
    <input type="file", name="avatar">
    <input type="submit" value="Upload">
</form>

服务端

var form = new multiparty.Form();
var avatar;

form.on('error', next);
form.on('close', function () {
	res.send('xxx');
});

form.on('field', function(name, val){
    if (name !== 'title') return;
    title = val;
  });

console.log('1');
form.on('part', function (part) {

	console.log('2');
	if (!part.filename) return ;
	if (part.name != 'avatar') return part.resume();
	avatar = {};
	avatar.filename = part.filename;
	avatar.size = 0;
	part.on('data', function(buf){
		avatar.size += buf.length;
	});
});

form.parse(req);

得不到预想的图片,而且我把req console.log出来并没有看到任何文件的信息,要怎么去看?难道是没上传上来? console.log出来的headers信息是这样 headers: { host: ‘xxx’, connection: ‘keep-alive’, ‘content-length’: ‘7811’, ‘cache-control’: ‘max-age=0’, accept: ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8’, origin: ‘http://xxx’, ‘user-agent’: ‘Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36’, ‘content-type’: ‘multipart/form-data; boundary=----WebKitFormBoundaryhzU9qAyW14ryNaOf’, referer: ‘http://xxx’, ‘accept-encoding’: ‘gzip,deflate,sdch’, ‘accept-language’: ‘en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4’, cookie: ‘connect.sid=s%3Am5iDWblBrlruE2ucuQds3I3UOsi-LSTq.hDncxPBALz96McBvQQkCtXYG9NR47%2FW%2BnwZ%2BfbXdo%2Fw’ }

2 回复

楼主还是先熟悉掌握下HTTP协议吧 ,有助于楼主更好的解决问题 :)

req哪里来的- -

回到顶部