npm install formidable
// 引入formidable第三方模块
const formidable = require('formidable');
const path = require('path');
module.exports = (req, res) => {
const form = formidable({
//multiples: true,
//保留上传文件的拓展名后缀
keepExtensions: true,
//配置上传文件的存放位置
uploadDir: path.join(__dirname, '../', '../', 'public', 'uploads')
});
// 4.解析表单
form.parse(req, (err, fields, files) => {
// 1.err错误对象 如果表单解析失败 err里面存储错误信息 如果表单解析成功 err将会是null
// 2.fields 对象类型 保存普通表单数据
// 3.files 对象类型 保存了和上传文件相关的数据
// res.send(files.cover.path.split('public')[1])
res.send(files)
});
// res.send('ok');
}
//文件上传提交表单时,表单数据必须以二进制方式上传
//enctype 指定表单数据的编码类型
默认值: enctype="application/x-www-form-urlencoded" //name=zhangsan&age=20
二进制: enctype="multipart/form-data"