Advertisement
ramonchang

Untitled

Nov 2nd, 2020
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const urls = []  
  2. var b =  await store(urls, "/blogImages/", files) // call the function here, the function returns an arr of urls
  3.  
  4.  
  5. async function uploadImageAsPromise (imageFile, location) {
  6.     return new Promise(function (resolve, reject) {
  7.         var storageRef = storage.ref(location+imageFile.name);
  8.         var task = storageRef.put(imageFile);
  9.  
  10.         //Update progress bar
  11.         task.on('state_changed',
  12.             function progress(snapshot){
  13.                 var percentage = snapshot.bytesTransferred / snapshot.totalBytes *
  14.                      100;
  15.                      console.log(percentage)
  16.             },
  17.             function error(err){
  18.                 console.log(err);
  19.                 reject(err);
  20.             },
  21.             function complete(){
  22.                 var downloadURL = task.snapshot.ref.getDownloadURL().then((url)=> {
  23.                    //console.log(url)
  24.                    resolve(url)
  25.                 });
  26.                 //console.log(task.snapshot, downloadURL)
  27.                 //resolve(downloadURL);
  28.             }
  29.         );
  30.     });
  31. }
  32.  
  33. async function store(arr, location, files){
  34.     let a = [...files.files]
  35.     // console.log(a)
  36.     a.forEach((element,i) => {
  37.         if(element == undefined || element.name == undefined){
  38.             let j = files.files.indexOf(element)
  39.             files.files.splice(j ,1)
  40.         }
  41.     });
  42.     // console.log(files.files)
  43.     if(files.files.length <1 || files.files[0] === undefined){
  44.         return
  45.     }
  46.     for (var i = 0; i < files.files.length; i++) {
  47.         if(files.files[i] === undefined){
  48.             continue
  49.         }
  50.         console.log(i, files.files.length, i === files.files.length)
  51.         if(i === files.files.length - 1){
  52.             var imageFile = files.files[i];
  53.             await uploadImageAsPromise(imageFile, location).then((res)=>{
  54.                 console.log(res)
  55.                 arr.push(res)
  56.                 console.log(arr);
  57.             })
  58.             return arr
  59.         }
  60.         var imageFile = files.files[i];
  61.         await uploadImageAsPromise(imageFile, location).then((res)=>{
  62.          arr.push(res);
  63.           });
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement