SHARE
TWEET

Untitled

a guest May 24th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var metadata = {
  2.   contentType: 'image/png'
  3. };
  4.  var storageRef = firebase.storage().ref("image/" + file);
  5. // Upload file and metadata to the object 'images/mountains.jpg'
  6. var uploadTask = storageRef.put(file, metadata);
  7.  
  8. // Listen for state changes, errors, and completion of the upload.
  9. uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
  10.   function(snapshot) {
  11.     // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
  12.     var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
  13.     console.log('Upload is ' + progress + '% done');
  14.     switch (snapshot.state) {
  15.       case firebase.storage.TaskState.PAUSED: // or 'paused'
  16.         console.log('Upload is paused');
  17.         break;
  18.       case firebase.storage.TaskState.RUNNING: // or 'running'
  19.         console.log('Upload is running');
  20.         break;
  21.     }
  22.   }, function(error) {
  23.  
  24.   // A full list of error codes is available at
  25.   // https://firebase.google.com/docs/storage/web/handle-errors
  26.   switch (error.code) {
  27.     case 'storage/unauthorized':
  28.       // User doesn't have permission to access the object
  29.       break;
  30.  
  31.     case 'storage/canceled':
  32.       // User canceled the upload
  33.       break;
  34.  
  35.    
  36.  
  37.     case 'storage/unknown':
  38.       // Unknown error occurred, inspect error.serverResponse
  39.       break;
  40.   }
  41. }, function() {
  42.   // Upload completed successfully, now we can get the download URL
  43.   uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
  44.     console.log('File available at', downloadURL);
  45.   });
  46. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top