Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Single file upload
  2. const file = this.$refs.file.files[0];
  3.  
  4. Vapor.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
  20. const files = this.$refs.file.files;
  21. const promises = []
  22.  
  23. files.map(f => promises.push(Vapor.store(f, { // multi-progressbar stuff would go here. })
  24.  
  25. // Wait for all uploads to complete...
  26. await Promise.all(promises)
  27.  
  28. // Make one post to your api route per file uploaded...
  29. promises.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...
  40. const results = []
  41.  
  42. promises.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.  
  52. axios.post('/api/multiple-files', results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement