Skip to content

Instantly share code, notes, and snippets.

@malikkurosaki
Last active June 30, 2019 09:26
Show Gist options
  • Save malikkurosaki/c80800af4aca42fd373e2310f2f31cfc to your computer and use it in GitHub Desktop.
Save malikkurosaki/c80800af4aca42fd373e2310f2f31cfc to your computer and use it in GitHub Desktop.
lcontoh firebase storage javascript

javascript storage javascript

deklarasi script

script(src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js")

script(src="https://www.gstatic.com/firebasejs/5.10.1/firebase-storage.js")

var firebaseConfig = {
      apiKey: "AIzaSyCckvX1DUrHlG6Zh6zVExgLOE_Mr4RANhw",
      authDomain: "probussystem-32ab2.firebaseapp.com",
      databaseURL: "https://probussystem-32ab2.firebaseio.com",
      projectId: "probussystem-32ab2",
      storageBucket: "probussystem-32ab2.appspot.com",
      messagingSenderId: "491305505631",
      appId: "1:491305505631:web:0b78fc6ed3d354b0"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);

  var storage = firebase.storage();
  var storageRef = storage.ref();
  var imagesRef = storageRef.child('images');

proses penguploatan

$("#gambar").change((e)=>{
        var file = e.target.files[0]
        var uploadTask = storageRef.child('images/rivers.jpg').put(file);

        uploadTask.on('state_changed', function(snapshot){
                // Observe state change events such as progress, pause, and resume
                // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
                var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
                console.log('Upload is ' + progress + '% done');
                switch (snapshot.state) {
                    case firebase.storage.TaskState.PAUSED: // or 'paused'
                        alert("upload paused")
                    break;
                    case firebase.storage.TaskState.RUNNING: // or 'running'
                        console.log('Upload is running');
                    break;
                }
            }, function(error) {
                //error
                alert("upload error")
            }, function() {
                // Handle successful uploads on complete
                // For instance, get the download URL: https://firebasestorage.googleapis.com/...
                uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
                    alert("image uploaded")
                        console.log('File available at', downloadURL);
                    });
            });
    })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment