Vue的手机拍照相册照片上传
一个小demo
支持手机相册和拍照上传

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<template>
<div>
<input
id="upload_file"
type="file"
style="display: none;"
accept="image/*"
name="file"
@change="fileChange($event)"
/>
<div class="upload_warp">
<div class="upload_warp_img">
<div class="upload_warp_img_div" v-for="(item, index) in imgList">
<div class="upload_warp_img_div_top">
<img
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAkFBMVEX///8nJjYeHS8bGi0gHzAjIjMYFyvy8vMdGy4WFSnt7e7h4eMlJDXm5ujq6usXFirb2934+PlDQk9cW2ZNTFg+PUvT09YSECdpaHIAABxWVWBJSFRsa3VRUFwPDSXNzdA1NEMwLz+urrNhYGuLi5K+vcJ/f4eenqSqqq+Hh46YmJ56eYHEw8cAACEAABmrq7AxYxi+AAALtklEQVR4nN1d2WKiShQUUFFAxRXjSlziTOLM/P/fXdAYkaWKpYHm1qM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LN6Rn92s5hm2ruN9ksX4WZL7qOf1hr9FjZbF+qmBdh5SIiqn3+s/4e6qo7ZYEW8hS8V7F1dqrP/cRAPGwVHv9Z1wh0xG9ipcRZNafYgBHan13N3UfvWYr08z6zxZWsL5jgj8gh1KI9Z9ZpWgJjl2TARxeDb8SBSW5b7VwsPUn15o94z5Yl9FHscfD8EQ/25MoKsflhxv2WMWEms8H7IN1Gn0UOKJau7i+yIy+rpPICdjjBYhedO2GGX0lVWqzgCzF78JNjhj96EMuBX3gKawVWoFjCpZa6zsvFgoewAVVJDbh/JYpyDxBTCNg/WeNDLbrpIFArf+bIhuLSqqgD7IC9z0RMvCMvu7pEsaij1W8WcAXvF7n/K6bBAaxfr+YhOminE4jqUYyccALu531sHVBEspn9FHghY3+pTUF78to9FFA6+/MWi5QUEqjj2IBF/xbgKDENvEKaP3JDOU1+ijsUTLF1jwhlDqnpijoY5F826a1jG/DTYiiQSy6SRRb59iMCI4kRVzTI2mp+L01mMSMSruNCTJPxFu/uvGrRkcVlKCQcnbEzfotax+X/6jbqCDzRIz13ydQ5volYay1k3ywnYxFJ0SxrdwPTprTAEVV/dVMBX1cX8PNM3uTftKcXseyOm3tbXuu9RkLwnadn8DZcYLpqYzLZuK689XHtb6nEwL90+3eamG3R+4ptBM4MG2zCVMJBvPXca4o7vHX9xWX/wCc1c1uhrLWkwAAAABJRU5ErkJggg=="
class="upload_warp_img_div_del"
@click="fileDel(index)"
/>
</div>
<img :src="item.file.src" style="display: inline-block;" />
</div>
<div
class="upload_warp_left"
id="upload_warp_left"
@click="fileClick()"
v-if="this.imgList.length < 6"
>
<!--<img src="../assets/upload.png">-->
<div>相册上传</div>
</div>
</div>
</div>
<div class="upload_warp_text">
<span
>选中{{ imgList.length }}张文件,共{{ bytesToSize(this.size) }}</span
>
</div>
</div>
</template>
<script type="text/ecmascript-6">
export default {
name: "cameras-and-albums",
data(){
return{
imgList: [],
datas: new FormData(),
files:0,
size:0
}
},
methods:{
//调用相册&相机
fileClick() {
$('#upload_file').click();
},
//调用手机摄像头并拍照
getImage() {
let cmr = plus.camera.getCamera();
cmr.captureImage(function(p) {
plus.io.resolveLocalFileSystemURL(p, function(entry) {
compressImage(entry.toLocalURL(),entry.name);
}, function(e) {
plus.nativeUI.toast("读取拍照文件错误:" + e.message);
});
}, function(e) {
}, {
filter: 'image'
});
},
//从相册选择照片
galleryImgs() {
plus.gallery.pick(function(e) {
let name = e.substr(e.lastIndexOf('/') + 1);
compressImage(e,name);
}, function(e) {
}, {
filter: "image"
});
},
//点击事件,弹出选择摄像头和相册的选项
showActionSheet() {
let bts = [{
title: "拍照"
}, {
title: "从相册选择"
}];
plus.nativeUI.actionSheet({
cancel: "取消",
buttons: bts
},
function(e) {
if (e.index == 1) {
this.getImage();
} else if (e.index == 2) {
this.galleryImgs();
}
}
);
},
fileChange(el) {
this.files=$("#upload_file").get(0).files;
console.log(this.files.length);
for(let i=0;i<this.files.length;i++){
this.datas.append("file",this.files[i]);
}
this.show1=false;
console.log(typeof this.files);
console.log(this.files);
if (!el.target.files[0].size) return;
this.fileList(el.target);
el.target.value = ''
},
fileList(fileList) {
let files = fileList.files;
for (let i = 0; i < files.length; i++) {
//判断是否为文件夹
if (files[i].type != '') {
this.fileAdd(files[i]);
} else {
//文件夹处理
this.folders(fileList.items[i]);
}
}
},
//文件夹处理
folders(files) {
let _this = this;
//判断是否为原生file
if (files.kind) {
files = files.webkitGetAsEntry();
}
files.createReader().readEntries(function (file) {
for (let i = 0; i < file.length; i++) {
if (file[i].isFile) {
_this.foldersAdd(file[i]);
} else {
_this.folders(file[i]);
}
}
})
},
fileAdd(file) {
//总大小
this.size = this.size + file.size;
//判断是否为图片文件
if (file.type.indexOf('image') == -1) {
file.src = 'wenjian.png';
this.imgList.push({
file
});
} else {
let reader = new FileReader();
reader.vue = this;
reader.readAsDataURL(file);
reader.onload = function () {
file.src = this.result;
this.vue.imgList.push({
file
});
}
}
},
fileDel(index) {
this.size = this.size - this.imgList[index].file.size;//总大小
this.imgList.splice(index, 1);
},
bytesToSize(bytes) {
if (bytes === 0){
return '0 B';
}
let k = 1000, // or 1024
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
},
dragenter(el) {
el.stopPropagation();
el.preventDefault();
},
dragover(el) {
el.stopPropagation();
el.preventDefault();
},
drop(el) {
el.stopPropagation();
el.preventDefault();
this.fileList(el.dataTransfer);
},
shows(et,tx){
this.strut=et;
this.txt=tx;
},
handleClick(){
this.$store.commit('add')
},
},
}
</script>
<style lang="">
.upload_warp_img_div_del {
position: absolute;
top: 6px;
width: 16px;
right: 4px;
}
.upload_warp_img_div_top {
position: absolute;
top: 0;
width: 100%;
height: 30px;
background-color: rgba(0, 0, 0, 0.4);
line-height: 30px;
text-align: left;
color: #fff;
font-size: 12px;
text-indent: 4px;
}
.upload_warp_img_div_text {
white-space: nowrap;
width: 80%;
overflow: hidden;
text-overflow: ellipsis;
}
.upload_warp_img_div img {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
}
.upload_warp_img_div {
position: relative;
height: 100px;
width: 120px;
border: 1px solid #ccc;
margin: 0px 5px 5px 0px;
float: left;
line-height: 100px;
display: table-cell;
text-align: center;
background-color: #eee;
cursor: pointer;
}
.upload_warp_img {
border-top: 1px solid #D2D2D2;
padding: 5px 0 0 5px;
overflow: hidden
}
.upload_warp_text {
text-align: left;
margin-bottom: 5px;
padding-top: 5px;
text-indent: 14px;
border-top: 1px solid #ccc;
font-size: 14px;
}
.upload_warp_right {
float: left;
width: 57%;
margin-left: 2%;
height: 100%;
border: 1px dashed #999;
border-radius: 4px;
line-height: 130px;
color: #999;
}
.upload_warp_left button {
margin: 8px 5px 0px 5px;
cursor:pointer;
}
.upload_warp_left {
float: left;
}
.upload_warp {
margin: 5px;
}
</style>