(node.js 学习二)利用formidable处理文件上传

利用formidable 接收和上传文件的HTTP服务器

(1) 通过npm安装formidable; (2) 创建一个IncomingForm实例; (3) 调用form.parse()解析HTTP请求对象; (4) 监听表单事件field、file和end;

先安装formidable npm install formidable

var formidable = require('formidable');
var form = new formidable.IncomingForm();
form.parse(req,function(err,fields,files){
	console.log(fields);
	console.log(files);
	res.end('上传完成');
});

//计算上传进度
form.on('progress',function(bytesReceived,bytesExpected){
	var percent = Math.floor(bytesReceived/bytesExpected*100);
	console.log(percent);
});
Loading Disqus comments...
Table of Contents