-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileUpload.js
97 lines (91 loc) · 2.83 KB
/
FileUpload.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
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
var FileUpload = {
data() {
return {
id: 0,
files: [],
}
},
methods: {
addFiles: function () {
this.$refs.files.click();
},
getunUploadedFile: function () {
return this.files.filter(item => {
return item.uploaded == false;
});
},
sendToServer: async function (formData, index, id) {
await axios.post(this.url,
formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then(response => {
this.updateSuccess(response.data,id);
}).catch(function (error) {
console.log(error);
});
},
submitFiles(index=0) {
let files = this.getunUploadedFile();
let formData = new FormData();
if (index >= files.length) {
return;
};
let file = files[index].file;
formData.append('file', file);
// console.log('index '+index+'Id '+files[index].id);
this.sendToServer(formData, index, files[index].id);
this.submitFiles(index + 1);
},
updateSuccess: function (data,id) {
let item = this.getItem(id);
// console.log("Id"+id);
// console.log(item);
let index = this.files.indexOf(item[0]);
// console.log(index);
// console.log(item+"Strin");
this.files[index].uploaded = true;
this.files[index].data = data;
// console.log(item);
},
loadData: function (file, url) {
// console.log('file'+url);
this.files.unshift({
id: this.id,
file: file,
url: url,
uploaded: false,
data: {}
});
this.id = this.id + 1;
},
handleFilesUpload() {
let files = this.$refs.files.files;
var reader = new FileReader();
let vm = this;
function readFile(index) {
if (index >= files.length) {
return vm.submitFiles();
};
var file = files[index];
reader.onload = function (e) {
vm.loadData(file, reader.result)
readFile(index + 1)
}
reader.readAsDataURL(file);
}
readFile(0)
},
removeFile(key) {
this.files.splice(key, 1);
},
getItem: function (key) {
return this.files.filter(item => {
return item.id==key;
});
}
}
}
export {FileUpload}