FileUpload.vue
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<template>
<div>
<el-upload
:ref="upload.ref"
multiple
:accept="upload.acceptFile"
action
:on-change="handleImportChanged"
:disabled="upload.isUploading"
:on-progress="handleFileUploadProgress"
:on-success="handleSuccess"
:auto-upload="false"
drag
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">
将文件拖到此处,或
<em>点击上传</em>
</div>
<div class="el-upload__tip" slot="tip">
</div>
<div class="el-upload__tip" style="color: red" slot="tip">
<p style="font-size:16px">温馨提示:</p>
<p>1、文件目前仅支持xlsx,xls,csv格式</p>
<p>2、导入数据量大时请耐心等待</p>
<p>3、数据导入中,请勿关闭本页面</p>
</div>
</el-upload>
<div slot="footer" class="dialog-footer" style="text-align:center;margin-top:20px;">
<el-button type="primary" @click="submitForm">确 定</el-button>
<el-button @click="cancel">取 消</el-button>
</div>
</div>
</template>
<script>
import {UploadImport} from '@/utils/excel'
export default {
name:"FileUpload",
props:[
'upload',
],
data() {
return {
};
},
computed: {
},
methods: {
handleImportChanged(files,fileList){
this.upload.file=files.raw;
if(fileList.length>1){//单文件上传,后者覆盖前者 配合参数设置 1去除limit=1设置 2允许多传 multiple
fileList.splice(0,1);
}
},
submitForm () { // 提交上传文件
if(this.checkFile(this.upload.file)){
// this.$emit('onFileSubmit',this.upload.file);
UploadImport(this.upload.file).then((data)=>{
this.$emit(this.upload.emitEventName,data);
// this.dealFile(data)
this.handleSuccess();
}).catch((err)=>{
this.$message.error(this.upload.submitErrorMsg);
this.handleSuccess();
console.log('err',err);
});
}
else{
return false;
}
},
cancel(){
this.upload.open=false;
},
checkFile(){
let fileSuffix=this.upload.file.name.substring(this.upload.file.name.lastIndexOf('.')+1);
console.log('whiteList',this.upload.whiteList);
if(this.upload.whiteList.indexOf(fileSuffix)===-1){
this.$message.error(this.upload.formatMsg);
return false;
}
else{
return true
}
},
handleSuccess () { // 文件上传成功处理
this.upload.isUploading = false
this.$refs.upload.clearFiles()
// this.upload.open = false
},
handleFileUploadProgress () {// 文件上传中处理
this.upload.isUploading = true
},
},
created() {
},
};
</script>