Firebase如何在断开连接后从存储中删除图像?

时间:2018-05-14 05:22:29

标签: javascript jquery firebase firebase-storage

您好我在连接断开连接时尝试删除已上传的图像。

在下面的代码中,我尝试在断开连接期间从存储中删除图像。然而," storageRef.delete();"即使连接仍然有效,也会发生。



    var connectedRef = database.ref(".info/connected");
    var connectionsImgRef = database.ref("/imageConnections");
    
    var imgCon = "";
    connectedRef.on("value", function (snap) {
        if (snap.val()) {
            $("#submitImage").on("click", function () {
                event.preventDefault();

                var fileName = selectedFile.name;
                storageRef = firebase.storage().ref("images/" + fileName);
                
                var uploadTask = storageRef.put(selectedFile);
                uploadTask.on('state_changed', function (snapshot) {
                    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) {
                
                }, function () {
               uploadTask.snapshot.ref.getDownloadURL().then(function (downloadURL) {
                        console.log('File available at', downloadURL);

                        userImgURL = downloadURL;

                        imgCon = connectionsImgRef.push(userImgURL);

                        userDisplayImg.attr("src", downloadURL);
                        userDisplayImg.addClass("userImage")
                        $("#uploadedImg").html(userDisplayImg);

                        //Issue: the remove is initiated even before disconnect.
                        imgCon.onDisconnect().remove(() => {
                            console.log("call back initiated")
                            storageRef.delete();
                        });
                        
                        //Issue: the remove is initiated even before disconnect.
                        // imgCon.onDisconnect().remove(function () {
                        //     storageRef.delete();
                        // });
                    })

                });
            });
        };
    });
             

<!-- Note this is using bootstrap's classes. -->

<input type="file" class="custom-file-input upload-group" id="file">
<label class="custom-file-label" for="validatedCustomFile">Upload an image!</label>

<button id="submitImage" class="btn btn-light">Upload</button>
&#13;
&#13;
&#13;

0 个答案:

没有答案