使用node.js的formidable模块,运行到parse后程序不执行。

const formidable = require('formidable');
const path = require('path');

module.exports = (req, res) => {
	// 1.创建表单解析对象
	const form = new formidable.IncomingForm();
	// 2.配置上传文件的存放位置
	form.uploadDir = path.join(__dirname, '../', '../', 'public', 'uploads');
	// 3.保留上传文件的后缀
	form.keepExtensions = true;
	// 4.解析表单
	form.parse(req,  (err, fields, files) => {
		// 1.err错误对象 如果表单解析失败 err里面存储错误信息 如果表单解析成功 err将会是null
		// 2.fields 对象类型 保存普通表单数据
		// 3.files 对象类型 保存了和上传文件相关的数据
		
		res.send(fields);
	})
	console.log(111);
}

浏览器一直在打圈圈,没有收到任何响应,但终端输出了111,说明程序运行时直接跳过了parse的部分。

查了一圈发现是,需要提交的form,要加一个属性enctype的属性。

<form action='/article' method='post' enctype="multipart/form-data">
<input type="file">
</form>

enctype指定表单的数据编码类型。表单的默认类型为 application/x-www-form-urlencoded,如 name=zhangsan&age=20。如果表单中有需要上传的文件(<input type="file">),需要将表单数据类型编码为2进制类型,使用 multipart/form-data。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值