async UPLOAD_IMAGES() {
      try {
        let self = this;
        var storageRef = firebase.storage().ref();
        var files = document.getElementById("photoupload").files;
        var file = files[0];
        // Create the file metadata
        var metadata = {
          contentType: "image/jpeg"
        };

        // Upload file and metadata to the object 'images/mountains.jpg'
        var uploadTask = storageRef
          .child(`${this.name}/` + file.name)
          .put(file, metadata);

        // Listen for state changes, errors, and completion of the upload.
        uploadTask.on(
          firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
          function(snapshot) {
            // 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'
                console.log("Upload is paused");
                break;
              case firebase.storage.TaskState.RUNNING: // or 'running'
                console.log("Upload is running");
                break;
            }
          },
          function(error) {
            // A full list of error codes is available at
            // https://firebase.google.com/docs/storage/web/handle-errors
            switch (error.code) {
              case "storage/unauthorized":
                // User doesn't have permission to access the object
                break;

              case "storage/canceled":
                // User canceled the upload
                break;
              case "storage/unknown":
                // Unknown error occurred, inspect error.serverResponse
                break;
            }
          },
          function() {
            // Upload completed successfully, now we can get the download URL
            uploadTask.snapshot.ref
              .getDownloadURL()
              .then(function(downloadURL) {
                console.log("File available at", downloadURL);
                self = downloadURL;
              });
          }
        );
      } catch (error) {
        console.log("ERR ===", error);
        alert("Image uploading failed!");
      }
    }

यह मेरा छवि अपलोड के लिए पूर्ण कार्य है। मेरे पास केवल एक ही समस्या है कि इस फ़ंक्शन के अंदर कॉलबैक फ़ंक्शन हैं।

 function() {
            // Upload completed successfully, now we can get the download URL
            uploadTask.snapshot.ref
              .getDownloadURL()
              .then(function(downloadURL) {
                console.log("File available at", downloadURL);
                self = downloadURL;
              });
          } 

केवल इस भाग के लिए मैं async प्रतीक्षा फ़ंक्शन प्राप्त कर सकता हूं?

2
Pathum Kalhan 23 अप्रैल 2019, 10:12

1 उत्तर

यदि किसी को कॉलबैक के बजाय प्रतीक्षा/async का उपयोग करते समय अपलोड प्रगति का निरीक्षण करने की आवश्यकता है, तो यह कोड काम करता है:

try {
   await saveToFirebase();
   alert("Happy days!");
} catch (error) {
   alert("Sad days!");
}


async function saveToFirebase() {
   // Create promise.
   let promise = $.Deferred();

   // Set storage reference.
   let storageRef = firebase.storage().ref();

   // Se file path.
   let filepath = "your-path.txt";

   // Set content type.
   let metadata = {
      contentType: "text/plain",
      cacheControl: "no-cache, max-age=0"
   };

   // Start upload.
   let uploadTask = storageRef.child(filepath).putString(data, "raw", metadata);

   // Monitor state of upload operation.
   uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, function(snapshot) {
      // Get task progress, including number of bytes uploaded and total number of bytes to be uploaded.
      let progress = (snapshot.bytesTransferred / snapshot.totalBytes);
      
      // Display progress to user.

   // Error during upload?
   }, function(error) {
      promise.reject(error);

   // Nope, upload succeeded.
   }, function() {
      promise.resolve();
   });

   // Return promise.
   return promise;
}
2
Crashalot 26 फरवरी 2021, 07:43