主要是想用http-request覆盖默认的上传行为,可以自定义上传
<el-form-item label="镜像上传:" prop="name">
<el-upload
class="upload-demo"
action="#"
:http-request="customUpload"
multiple
:limit="3"
:on-exceed="handleExceed"
:file-list="fileList"
:before-upload="beforeAvatarUpload"
accept=".tar">
<el-button size="small" type="primary">点击上传</el-button>
</el-upload>
</el-form-item>
async customUpload(file) {
this.loading = true
this.loadingText = '上传中'
let that = this
return new Promise(async (resolve, reject) => {
if (!file) {
alert('请先选择一个文件');
return;
}
const formData = new FormData();
formData.append('file', file.file);
try {
const response = await request.post('/virtual/file/uploadDockerTar', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
resolve('上传成功');
that.$message.success('上传成功')
that.getList();
that.loading = false
} catch (error) {
reject(error);
that.loading = false
}
})
},
handleExceed(files, fileList) {
this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
},
beforeAvatarUpload(file) {
const isTAR = file.type === 'application/x-tar';
if (!isTAR) {
this.$message.error('上传文件只能是 tar 格式!');
}
return isTAR;
},