upload.vue
3.59 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<style>
.upload-iframe {
display: none;
}
.upload-btns {
/*display: inline-block;*/
/*position: absolute;*/
left:0px;
bottom:0px;
/*overflow: hidden;*/
}
.upload-btns input[type="file"] {
position: absolute;
top: 0;
left: 0;
width: 35px;
height: 35px;
font-size: 18px;
opacity: 0;
cursor: pointer;
}
</style>
<template>
<!-- <section style="margin-top:0px;"> -->
<my-tag :tag-name="tag" v-if="type == 'file'" :class="class"
:style="style">
<iframe :id="iframeName" :name="iframeName" @load="loaded" class="upload-iframe"></iframe>
<form :id="formId" action="/file/upload/file" method="post" :target="iframeName" enctype="multipart/form-data">
<slot>{{label}}</slot>
<input type="file" name="uploadFile" @change="upload">
</form>
</my-tag>
<my-tag :tag-name="tag" :class="class" v-else>
<iframe :id="iframeName" :name="iframeName" @load="loaded" class="upload-iframe"></iframe>
<form :id="formId" action="/file/upload/image" method="post" :target="iframeName" enctype="multipart/form-data">
<slot>{{label}}</slot>
{{style}}
<input type="file" name="uploadFile" @change="upload">
<input type="hidden" name="type" v-model="type">
</form>
</my-tag>
<!-- </section> -->
</template>
<script>
module.exports = {
props: {
style:{
type:String
},
label: {
type: String,
default: ''
},
class: {
type: Array,
default: function () {
return [];
},
coerce: function (val) {
return ['upload-btns'].concat(val || []);
}
},
tag: {
type: String,
default: 'div'
},
// 业务属性值,最后会传送回去
ywtag:{
type: String,
default: ''
},
model: {
type: String,
require: true,
twoWay: true
},
type: {
type: String,
default: 'img'
},
evtname:{
type: String,
default: 'upload'
},
multiple: false
},
data: function () {
return {
iframeName: 'uploadVue' + Math.random().toString(32).substr(2),
formId: 'upload-form-' + Math.random().toString(32).substr(2)
}
},
methods: {
loaded: function (event) {
var content = "";
var pre=event.target.contentWindow.document.body.querySelector('pre') || '';
if(!pre){
pre = document.getElementById(this.iframeName).contentWindow;
content = pre.document.body.innerText || '';
if(content==''){
return ;
}
}else{
if(!pre){
return ;
}
content=pre.innerHTML;
}
try {
var result = JSON.parse(content);
/**
返回的是一个对象,然后获得内容就可以
格式:
obj.filepath : 文件路径
obj.ywtag : 业务属性值
**/
var backObj = {};
if (result.errorCode==0) {
// 文件的路径,是数组的方式
var fileJsonStr = "";
if(result.data!=null && result.data.length>0){
fileJsonStr = JSON.stringify(result.data);
}
backObj.filepath = fileJsonStr;
backObj.ywtag = this.ywtag;
this.model = fileJsonStr;
// this.$dispatch('file-change', fileJsonStr);
this.$dispatch('file-change', backObj);
} else {
this.MessageBox.alert(result.message);
}
} catch(e) {
this.MessageBox.alert(e.message);
}
},
upload: function (event) {
document.getElementById(this.formId).submit();
}
}
};
</script>