vue+elementui el-upload上传文件携带参数
<el-upload ref="upload" style="display:inline;margin-left:10px;" action="url" :auto-upload="false" :multiple="false" :show-file-list="false" :before-upload="beforeUpload" :on-change="changeFile" accept=".xlsx,.xls" :file-list="fileList"> </el-upload> export function parsingExcelAddData (data, setId) { return fetch({ url: ‘‘, method: ‘post‘, // 方式一定是post headers: { ‘Content-Type‘: ‘multipart/form-data‘, ‘setId‘: setId //放到formData里后端拿不到,所以放到请求头里了 }, data: data // 参数需要是单一的formData形式 }) } beforeUpload(file) { let filename = file.name let arr = filename.split(‘.‘) if (arr[1] !== ‘xls‘ && arr[1] !== ‘xlsx‘) { this.$message.error(‘上传文件只能是 excel/xls 格式!‘) return false } return arr }, changeFile(file,fileList) { let fd = new FormData(); fd.append(‘file‘,file.raw);//传文件 let self = this parsingExcelAddData(fd,this.dataObj.setId).then(function(res){ if (res.data.success) { self.$message({ type: ‘success‘, message: ‘上传成功‘ }) self.currentNodeData(self.dataObj.setId) //此方法是重新刷数据,可以自己写 } else { self.$message({ type: ‘warning‘, message: res.data.msg }) return false } }) }
文章来自:https://www.cnblogs.com/smile-fanyin/p/12431591.html