前端处理

使用 sprk-md5.js plupload.js 来完成上传的功能

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
# 引入 下面 Uploader.vue 的封装组件
<uploader
// 配置外部的点击以获取dom节点对象
browse_button="browse_button"
// 配置上传地址
:url="server_config.url+'/BigFile/'"
// 分片上传每次上传的大小
chunk_size="2MB"
// 禁止选择重复的文件
:filters="{prevent_duplicates:true}"
// 添加文件以及其他有关参数信息r
/>

watch: {
// 监听变化添加 tableData 显示列表
files: {
handler() {
this.tableData = [];
this.files.forEach((e) => {
this.tableData.push({
name: e.name,
size: e.size,
status: e.status,
id: e.id,
percent: e.percent
});
});
},
deep: true
}
},


// 获取当前实例对象
inputUploader(up) {
debugger;
console.log(up);
this.up = up;
this.files = up.files;
},

// uploader为当前的plupload实例对象,files为一个数组,里面的元素为本次添加到上传队列里的文件对象
filesAdded(up, files) {
files.forEach((f) => {
f.status = -1;
FileMd5(f.getNative(), (e, md5) => {
f["md5"] = md5;
f.status = 1;
});
});
},
deleteFile(id) {
debugger;
console.log(this.up);
let file = this.up.getFile(id);
this.up.removeFile(file);
},
// 添加对应信息
beforeUpload(up, file) {
up.setOption("multipart_params", {"size":file.size,"md5":file.md5});
}
}

file-md5.js

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
'use strict';

import '../plugins/js-spark-md5.js'

export default function (file, callback) {
var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
file = file,
chunkSize = 2097152, // Read in chunks of 2MB
chunks = Math.ceil(file.size / chunkSize),
currentChunk = 0,
spark = new SparkMD5.ArrayBuffer(),
fileReader = new FileReader();
// 分块处理
fileReader.onload = function (e) {
console.log('read chunk nr', currentChunk + 1, 'of', chunks);
spark.append(e.target.result); // Append array buffer
currentChunk++;

if (currentChunk < chunks) {
loadNext();
} else {
callback(null, spark.end());
console.log('finished loading');
}
};

fileReader.onerror = function () {
callback('oops, something went wrong.');
};
// 分块数据存储
function loadNext() {
var start = currentChunk * chunkSize,
end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;

fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
}

loadNext();
};


file-url.js

1
2
3
4
5
6
7
8
9
10
11
12
export default function (file, callback) {
if (!file || !/image\//.test(file.type)) return;
let fileReader = new FileReader();
fileReader.onload = function () {
callback(null,fileReader.result);
};
fileReader.onerror = function () {
callback('oops, something went wrong.');
};
fileReader.readAsDataURL(file);
}

Uploader.vue
plupload封装为成一个vue组件

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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<template></template>
<script>
import plupload from 'plupload/js/plupload.full.min.js'
export default {
name: "Uploader",
props: {
browse_button: {
type: String
},
container: {
type: String
},
url: {
type: String
},
filters: {
type: Object
},
headers: {
type: Object
},
multipart_params: {
type: Object
},
resize: {
type: Object
},
drop_element: {
type: String
},
required_features: {
type: String
},
runtimes: {
type: String,
default() {
return "html5,flash,silverlight,html4";
}
},
chunk_size: {
type: String,
default() {
return "0";
}
},
multipart: {
type: Boolean,
default(){
return true;
}
},
max_retries: {
type: Number,
default() {
return 0;
}
},
multi_selection: {
type: Boolean,
default() {
return true;
}
},
unique_names: {
type: Boolean,
default() {
return false;
}
},
file_data_name: {
type: String,
default() {
return "file";
}
},
flash_swf_url: {
type: String,
default() {
return 'plupload/js/Moxie.swf';
}
},
silverlight_xap_url: {
type: String,
default() {
return "plupload/js/Moxie.xap";
}
},
Init: {
type: Function
},
PostInit: {
type: Function
},
Browse: {
type: Function
},
OptionChanged: {
type: Function
},
Refresh: {
type: Function
},
StateChanged: {
type: Function
},
UploadFile: {
type: Function
},
BeforeUpload: {
type: Function
},
QueueChanged: {
type: Function
},
UploadProgress: {
type: Function
},
FilesRemoved: {
type: Function
},
FileFiltered: {
type: Function
},
FilesAdded: {
type: Function
},
FileUploaded: {
type: Function
},
ChunkUploaded: {
type: Function
},
UploadComplete: {
type: Function
},
Error: {
type: Function
},
Destroy: {
type: Function
}
},
data() {
return {
up: {},
}
},
watch: {
up(val) {
this.$emit('inputUploader', val);
}
},
methods: {
init() {
let that = this;
const preInitMethod = {
Init(up, info) {
if (that.Init != null) {
that.Init(up, info);
}
},
UploadFile(up, file) {
if (that.UploadFile != null) {
that.UploadFile(up, file);
}
}
};
const initMethod = {
PostInit() {
if (that.PostInit != null) {
that.PostInit();
}
},
Browse(up) {
if (that.Browse != null) {
that.Browse(up);
}
},
OptionChanged(up, name, value, oldValue) {
if (that.OptionChanged != null) {
that.OptionChanged(up, name, value, oldValue);
}
},
Refresh(up) {
if (that.Refresh != null) {
that.Refresh(up);
}
},
StateChanged(up) {
if (that.StateChanged != null) {
that.StateChanged(up);
}
},
BeforeUpload(up, file) {
if (that.BeforeUpload != null) {
that.BeforeUpload(up, file);
}
},
QueueChanged(up) {
if (that.QueueChanged != null) {
that.QueueChanged(up);
}
},
UploadProgress(up, file) {
if (that.UploadProgress != null) {
that.UploadProgress(up, file);
}
},
FilesRemoved(up, files) {
if (that.FilesRemoved != null) {
that.FilesRemoved(up, files);
}
},
FileFiltered(up, file) {
if (that.FileFiltered != null) {
that.FileFiltered(up, file);
}
},
FilesAdded(up, files) {
if (that.FilesAdded != null) {
that.FilesAdded(up, files);
}
},
FileUploaded(up, file, info) {
if (that.FileUploaded != null) {
that.FileUploaded(up, file, info);
}
},
ChunkUploaded(up, file, info) {
if (that.ChunkUploaded != null) {
that.ChunkUploaded(up, file, info);
}
},
UploadComplete(up, files) {
if (that.UploadComplete != null) {
that.UploadComplete(up, files);
}
},
Error(up, args) {
if (that.Error != null) {
that.Error(up, args);
}
},
Destroy(up) {
if (that.Destroy != null) {
that.Destroy(up);
}
}
};
let prop = {
runtimes: this.runtimes,
browse_button: this.browse_button,
container: this.container,
url: this.url,
chunk_size: this.chunk_size,
headers: this.headers,
multipart: this.multipart,
max_retries: this.max_retries,
multi_selection: this.multi_selection,
unique_names: this.unique_names,
file_data_name: this.file_data_name,
flash_swf_url: this.flash_swf_url,
silverlight_xap_url: this.silverlight_xap_url,
preinit: preInitMethod,
init: initMethod
};
if (this.filters != null) {
prop["filters"] = this.filters;
}
if (this.multipart_params != null) {
prop["multipart_params"] = this.multipart_params;
}
if (this.resize != null) {
prop["resize"] = this.resize;
}
if (this.drop_element != null) {
prop["drop_element"] = this.drop_element;
}
if (this.required_features != null) {
prop["required_features"] = this.required_features;
}
let uploader = new plupload.Uploader(prop);
uploader.init();
this.up = uploader;
}
},
mounted() {
console.log("uploader");
this.init();
}
}
</script>