0
  1. Upload was successful in the console.
const sendPost = () => {
  const id = uuid();
  const storage = getStorage();
  const storageRef = ref(storage, `posts/${id}`)
  const uploadTask = uploadString(storageRef, cameraImage, "data_url").then((snapshot) => {
    console.log('Uploaded a data_url string')
  });
}
  1. uploadTask.on is not a function (console error). I am trying to monitor the progress of the upload
uploadTask.on('state_changed', (snapshot) => {
  const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
  console.log("Upload is" + progress + "% done");
  switch (snapshot.state) {
    case "paused":
      console.log("upload is runnig")
      break;
  }
})
  1. trying to logout errors, if any
(error) => {
  switch (error.code) {
    case "storage/unauthorized":
      break;

    case "storage/canceled":
      break;

    case "storage/unknown":
      break;
  }
},
  1. Trying to get the uploaded file URL and post it as a doc to firebase db
() => {
  getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
    addDoc(collection(db, "posts"), {
      imageUrl: URL,
      username: "swizz6ix",
      read: false,
      profilePic: user.profilePic,
      timestamp: serverTimestamp(),
    })
    navigate("/chats")
  })
}, );
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Swizz 6ix
  • 23
  • 1
  • 5

1 Answers1

0

When you use uploadString(), the upload is not resumable. If you want to track upload progress then you'll have to use uploadBytesResumable() that takes a Blob | Uint8Array | ArrayBuffer as parameter and not a base64 string. So first you'll have to convert the base64 string into a Blob.

Checkout: How to convert Base64 String to javascript file object like as from file input form?

const fileToUpload = parseFileFromBase64();

const task = uploadBytesResumable(storageRef, fileToUpload)

task.on(...)
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84