项目场景:
使用element-ui中的el-upload组件时需要携带其他参数
=
解决方案
html:
<el-upload
action="/api/oss/file/add"
:headers="headers" // 如果头部需要传token
multiple
:limit="1" // 限制文件个数
:before-upload="handleBefore"
:on-success="handleSuccess"
:data="pdfData"
accept=".pdf" // 限制文件格式>
<el-button size="small" type="primary">上传PDF</el-button>
</el-upload>
js:
pdfData: {
'参数1': '',
'参数2': '',
'参数3': ''
},
headers: {
Authorization: Cookies.get('token')
//从cookie里获取token,并赋值 Authorization ,而不是token
}
// 上传前的回调函数
handleBefore(file) {
this.pdfData.参数1 = '值1';
this.pdfData.参数2 = '值2';
this.pdfData.参数3 = '值3';
}
// 上传成功回调
handleSuccess(res) {
if (res.status == 200) {
this.$message({
message: 'Success!',
type: 'success'
})
} else {
this.$message({
message: 'Upload Error!',
type: 'error'
})
}
}