How to cancel the upload of a file in the firebase web Storage?

Asked

Viewed 23 times

-1

I created a root storage reference in firebase web through the code below:

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

var uploadTask = storageRef.child('img-tour/' +files[0].name).put(files[0]);

However, I need to cancel sending the image if it is in the image. For example, if the image paisagem.jpg is in the firebase Storage and when trying to send it again, I want the upload to be canceled/stopped. How do I do this using firebase?

1 answer

0

first you can check if the file already exists.

const storageRef = firebase.storage().ref();
const fileRef = storageRef.child('img-tour/' +files[0].name);
fileRef.getDownloadURL().then(function(url) {
  // Arquivo já existe. Não enviar
}).catch(function(error) {
  if (error.code === 'storage/object-not-found') {
     // arquivo não existe.
     upload();
  } 
});

function upload() {
  fileRef.put(files[0]);
}

or you can create an Indice in the firestore database if you want to avoid direct checking in the Storage.

  • Thank you Paul for the force. Be in doubt what to put in "// File already exists. Do not send" in "// file does not exist." and in 'Storage/Object-not-found'.

  • if the getDownloadURL function returns a url in promisse. it means that there is a file for a given path. otherwise promisse is rejected and the error is equal to 'Storage/Object-not-found' is because the file does not exist in Storage, so you can send the file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.