使用 elementUi 的 upload 组件上传文件

同步改异步

element 的上传组件默认是同步的,不方便我们控制,所以我们把他调整成异步。

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
99
<template>
<div>
<el-dialog title="地图选点" :visible="this.mapBkg" width="50%" @close="cancel">
<div>
<el-upload
:action="this.url"
list-type="picture-card"
:before-upload="beforeUpload"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove">
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
</div>
<div slot="footer" class="dialog-footer">
<el-button @click="close">取 消</el-button>
<el-button type="primary" @click="open">确 定</el-button>
</div>
</el-dialog>
</div>
</template>

<script>
import {mapState,mapMutations,mapActions} from 'vuex'
export default {
name: "UploadBkgImg",
data() {
return {
url:'/api/ProjectManage/uploadImageOfProjPreview',
dialogImageUrl: '',
dialogVisible: false,
value: {},
formLabelWidth: '120px',
successData:''
};
},
computed:{
...mapState(['currentUrl','mapShow','mapEditShow','mapInputData','projectCode','dataList','mapBkg'])
},
methods:{
...mapMutations(['mapShowInfo','mapEditInfo','mapInputInfo','mapBkgShow']),
...mapActions(['getProjectCodeInfo','updateUnitProjXY']),
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
close() {
this.mapBkgShow(false);
},
open() {
this.mapBkgShow(false);
},
cancel() {
this.mapBkgShow(false);
},
beforeUpload(file) {
let status = 200;
let url = this.currentUrl+this.url;
let fd = new FormData();//通过formdata格式来传
fd.append("file", file); //传文件对象
fd.append("projCode", this.projectCode); //传其他参数
this.$http
.post(url, fd)
.then(res => {
let data = res.data.status;
if (data === status) {
this.mapBkgShow(false);
this.$message({
message: '提交成功',
type: 'success'
});
} else {
this.$message({
message: '提交失败,请刷新页面重新提交',
type: 'error'
});
}
})
.catch(error => {
console.log(error);
});
}
},
mounted() {


}
}
</script>

<style scoped>

</style>