score:1

import { storage } from "../../firebase"
import { ref, uploadBytes } from "firebase/storage";

const upload = (items) => {
  items.forEach((item, index) => {
    const storageRef = ref(storage, `/items/${fileName}`);
    const uploadTask = uploadBytes(storageRef, item.file);

    uploadTask.on(
      "state_changed",
      (snapshot) => {
        const progress =
          Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 1000)/10;
        console.log(`Upload #${index+1} is ${progress}% done`);
      },
      (error) => {
        console.log(error);
      },
      () => {
        console.log(`Upload #${index+1} is complete, fetching URL...`);
        getDownloadURL(storageRef)
          .then((url) => {
            console.log(`Upload #${index+1} is now available at ${url}.`);
            setMovie((prev) => {
              return { ...prev, [item.label]: url };
            });
            setUploaded((prev) => prev + 1);
          })
          .catch((error) => {
            console.log(error);
          });
      }
    );
  });
}

Related Query

More Query from same tag