<el-upload
ref="uploadfile"
class="upload-demo"
:action="uploadUrl"
:headers="{ Authorization: token }"
:auto-upload="true"
:show-file-list="false"
:on-progress="handleProgress"
:before-upload="handleBeforeUpload"
:on-success="uploadSuccess"
:on-error="uploadError"
:disabled="fileType.length == 0"
:multiple="false"
accept=".docx,.doc"
>
<el-button
size="small"
type="empty-button"
:disabled="fileType.length == 0"
>上传</el-button
>
</el-upload>
// 上传文件前判断
async handleBeforeUpload (file) {
let flag = true;
// 判断
let url = this.$apiurl.projectManage_projectFile_checkProjectReportDoc();
const paramsData = new FormData();
paramsData.append('file', file);
let checkResult = await this.doAxios.formdataUpload(url, paramsData, true, true, false);
if (checkResult.code == 200) {
flag = true;
} else {
this.$message.error(checkResult.message);
flag = false;
}
return false;
},
按照此写法,验证失败时仍然执行了上传,将判断改成如下方向即可:
handleBeforeUpload (file) {
return new Promise(async (resolve, reject) => {
let flag = true;
// 判断
let url = this.$apiurl.projectManage_projectFile_checkProjectReportDoc();
const paramsData = new FormData();
paramsData.append('file', file);
let checkResult = await this.doAxios.formdataUpload(url, paramsData, true, true, false);
if (checkResult.code == 200) {
flag = true;
} else {
this.$message.error(checkResult.message);
flag = false;
}
if (flag == true) {
resolve();
} else {
reject();
}
})
},