let multi = new Multipart({
fields:[{
name:‘token‘,
value:‘gdsgfhdgjukfdk‘
},{
name:‘username‘,
value:‘rainbowLover‘
}],
files:[{
filePath:‘http://example.png‘,//文件路径 必须要有的
filename:‘img1‘,//文件名称 后台要不要?
name: ‘name‘,//字段名
file: {object}
}],
header:{
‘Cookie‘: ‘cookievalue‘,
‘headerName‘: ‘headerValue‘
}
})
multi.files({
filePath:‘http://example.png‘,//文件路径 必须要有的
filename:‘img1‘,//文件名称 后台要不要?
name: ‘name‘,//字段名
file: {object}
})
multi.submit(url, { header })
.then(
(res) => {
console.log(res)
},
(err) => {
console.log(err)
}
)
/**
* 多文件上传
* url: 上传路径 必传
* data: 上传json参数 默认 {}
* jsonKey: 后台接收参数的key 默认 ‘jsonReport‘
* count: 文件最多上传几张 默认 9
* type: 选取文件类型 默认 all
* extension: 选取文件类型 字符串数组 根据文件拓展名过滤,仅 type==file 时有效。每一项都不能是空字符串。默认不过滤。默认 空
* header: 自定义请求头
*/
let options = {
url: ‘http://localhost:3000/upload‘,
data: {
name: "jeck",
age: 123
},
jsonKey: "jsonReport",
count: 9,
type: "all",
extension: [null],
header: {
"headerName": "headerValue"
}
}
npm i npm run build //打包生成库文件 npm run start //运行后端服务
完整代码:https://gitee.com/haipengguo/Multipart 如果对你有帮助请star
原文:https://www.cnblogs.com/rainbowLover/p/13056246.html