0

I have this code to upload images to firebase storage, but because I have problem if I upload the same file with the same name, I want every image to have unique names like image-v432bn4bn324nm.png

How I can do that ?? With functions ?? if (YES){ return How?? }

    addProsfora(prosfora, img) {

    // let aa = this.checkIfImageExist(img, 'prosfores');
    // console.log(aa);

    function onAfterAddingFile(img) {
      var fileExtension = '.' + img.name.split('.').pop();

      img.name = Math.random().toString(36).substring(7) + new Date().getTime() + fileExtension;

      console.log(img.name);
    };
    onAfterAddingFile(img);

    const storageRef = firebase.storage().ref();

    console.log(img);

    let path = `/${this.prosforesFolder}/${img.name}`;
    let iRef = storageRef.child(path);

    iRef.put(img).then((snapshot) => {

      prosfora.imageUrl = snapshot.downloadURL;
      prosfora.image = img.name;
      prosfora.path = path;
      // firebase.database().ref('/listings').push(listing);
      return this.prosfores.push(prosfora);
    });

  }
  • 2
    Possible duplicate of [Store files with unique/random names](http://stackoverflow.com/questions/37444685/store-files-with-unique-random-names) – Mike McDonald May 11 '17 at 16:43
  • You could try using the current UNIX timestamp and concatenating it with your hashed name. – Selene Blok Aug 18 '17 at 04:56

0 Answers0