上传功能是用element中的upload组件做的,里面有个属性控制上传文件格式
设置accept上传限制,同时在上传文件之前的钩子before-upload里面进行判断,当选择不符合格式文件时,进行提醒
代码
<el-upload
style="display: inline-block;"
class="upload-demo"
accept=".pdf,.jpg,.png"
action="Fake Action"
:before-upload="uploadSuccess"
:show-file-list="false"
:file-list="fileList">
<el-button size="mini" type="primary">点击上传</el-button>
<span slot="tip" class="el-upload__tip">支持pdf,jpg,png格式文件</span>
</el-upload>
uploadSuccess(file) {
// 截取上传文件的后缀名
let fileType = file.name.substring(file.name.lastIndexOf(".") + 1);
// 判断文件名的类型
if (fileType === 'pdf' || fileType === 'jpg' || fileType === 'png') {
const fd = new FormData();
fd.append('file', file)
const config = {
headers: {
'Content-Type': 'multipart/form-data'
}
}
axios.post(window.gapi + "/api/gemp/risktip/manage/upload/v1", fd, config).then((res) => {
if (res.data.status === 200) {
this.form.accessoryName = res.data.data
this.isUpload = true
}
}).catch((err) => {
console.log(err)
})
} else {
this.$message.error('上传文件仅支持pdf,jpg,png格式');
}
},