<el-upload :action="actionUrl" :data="ossData" :headers="headers" :before-upload="beforeUpload" :on-success="onSuccess" :on-error="onError" :on-change="onChange" :on-exceed="onExceed" :file-list="fileList" :limit="limit" accept=".xls,.xlsx">
//可以通过加这个input进行文件校验
<el-input size="mini" v-model="fileName" v-show="false">
</el-input>
</el-upload>
<script>
export default {
data() {
return {
actionUrl:'http:****',
ossData:{},
headers:{},
fileList:[],
limit:1,
}
},
methods: {
beforeUpload(file) {
let suffix = file.name.substring(file.name.lastIndexOf(".") + 1);
if (suffix != 'xls') {
this.$notify.warning({ message: "请上传指定的文件格式!", duration: 2000 })
return false;
}
this.ossData.id = 7;
},
onChange(file, fileList) {
if (file.size > 1024 * 1024 * 20) {
this.$notify.warning({ message: "请上传不超过 20M 大小的文件",})
return false;
}
if (fileList.length > 1) {
fileList = [fileList[1]]
}
this.fileList = fileList
this.$refs.upload.submit();
},
onSuccess(resp, file, fileList) {
},
文件上传失败时的钩子
onError(err, file, fileList) {
},
onExceed(files, fileList) {
this.$notify.error({ title: `请不要超过1个文件` });
},
}
}
</script>