· 6 years ago · Feb 14, 2020, 11:24 PM
1// Single file upload
2const file = this.$refs.file.files[0];
3
4Vapor.store(file, {
5 progress: progress => {
6 this.uploadProgress = Math.round(progress * 100);
7 }
8}).then(response => {
9 axios.post('/api/profile-photo', {
10 uuid: response.uuid,
11 key: response.key,
12 bucket: response.bucket,
13 name: file.name,
14 content_type: file.type,
15 })
16});
17
18
19// Multi-file upload
20const files = this.$refs.file.files;
21const promises = []
22
23files.map(f => promises.push(Vapor.store(f, { // multi-progressbar stuff would go here. })
24
25// Wait for all uploads to complete...
26await Promise.all(promises)
27
28// Make one post to your api route per file uploaded...
29promises.map((response, index) =>
30 axios.post('/api/file', {
31 uuid: response.uuid,
32 key: response.key,
33 bucket: response.bucket,
34 name: files[index].name,
35 content_type: files[index].type,
36 })
37);
38
39// Or make a single post to an api route that will accept an array of items, and map those accordingly...
40const results = []
41
42promises.map((response, index) =>
43 results.push({
44 uuid: response.uuid,
45 key: response.key,
46 bucket: response.bucket,
47 name: files[index].name,
48 content_type: files[index].type,
49 })
50);
51
52axios.post('/api/multiple-files', results)