简体   繁体   English

当我将图像上传到Firebase存储并检索下载URL并显示在img标签中时,它显示多次

[英]When I upload an image to firebase storage and retriving the download url and displaying in img tag but it is showing multiple times

I am uploading an image to firebase storage and again storing the URL path in the firebase database. 我将图像上传到Firebase存储,然后再次将URL路径存储在Firebase数据库中。 However when I upload the first time it is displaying a single time, but when I upload again to the gallery and select an image it is displaying two times. 但是,当我第一次上传时,它将显示一次,但是当我再次上传到图库并选择图像时,它将显示两次。 If again I go to the gallery and select an image it is displaying three times. 如果我再次去画廊,选择一张显示三遍的图像。 Please guide me how to solve this. 请指导我如何解决此问题。

<img id="image5e" src="img/upload.png" />
<input type='file' style="display:none;" name="photosubmit" id="photosubmit"/>
var app = {
    initialize: function() {
        app.bindEvents();    
    },
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {
         var uploadimg = document.getElementById("image5e");
         uploadimg.addEventListener("click",app.upload2gallery,false); 
    },         
    upload2gallery: function() {
     $("#photosubmit").click();

     var filebutton = document.getElementById("photosubmit");
     filebutton.addEventListener('change', function(e) {
         var file = e.target.files[0];    
         var storageRef = firebase.storage().ref('sweet_gift/' + file.name);    
         var task = storageRef.put(file);

         task.on('state_changed', function progress(snapshot) {
            var load = '<h1>Please Wait</h1>' + '<i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i><span>Loading ...</span>';
            $.mobile.loading('show', {
                text: 'Please wait...',
                textVisible: true,
                html: load,
                theme: 'z'
            });
        }, function error(err) {
        }, function complete() {
            $.mobile.loading('hide');

            var uni = localStorage.getItem("lunicode");
            var ref = firebase.database().ref(uni);
            var myname = storage.getItem("uname");
            var myid = storage.getItem("myid");
            var downloadURL = task.snapshot.downloadURL;
            ref.push({
                name: myname,
                imageurl: downloadURL,
                photoUrl: "/images/profile_placeholder.png",
                my_id: myid,
            });
        });
    });
},

$(document).on('pagebeforeshow', '#chatpage', function() {
    var uni = localStorage.getItem("lunicode");
    var ref = firebase.database().ref(uni);

    $("#images6").empty();

    ref.orderByChild("messages").on("child_added", function(snapshot) {
        $("#images6").append(movielist(snapshot.val()));
        var last_li = $(".cmsg li:last-child").offset().top;
        setTimeout(function() {
            $.mobile.silentScroll(last_li);
        }, 50);
        ActivityIndicator.hide();
    });
});

$(document).on('pagebeforehide', '#chatpage', function() {
    alert("going");
    var uni = localStorage.getItem("lunicode");
    var ref = firebase.database().ref(uni);
    $("#images6").empty();
    //ActivityIndicator.show();
    ref.orderByChild("messages").off("child_added");
    //ActivityIndicator.hide();
    $('#cmessage').val('');
});

As per Firebase Google support Engineer Sugestion, I put the bellow Event listner inside the DeviceReady function and it resolved the issue to me. 根据Firebase Google支持工程师Sugestion的要求,我将波纹管事件列表器放入DeviceReady函数中,它可以解决此问题。

Google Sugestion bellow Google Sugestion波纹管

[Can you move registering the file change handler on onDeviceReady? [您可以在onDeviceReady上注册文件更改处理程序吗? If you put it in upload2gallery, each time you click on the image, upload2gallery will be called, and so you are registering the file change event again, which results to your problem.] 如果将其放在upload2gallery中,则每次单击图像时,都会调用upload2gallery,因此您将再次注册文件更改事件,这将导致您的问题。]

var filebutton = document.getElementById("photosubmit");
 filebutton.addEventListener('change', function(e) {
     //Get The File
     var file = e.target.files[0];
     var storageRef = firebase.storage().ref('sweet_gift/' + file.name);
     var task = storageRef.put(file);
     task.on('state_changed',
         function progress(snapshot) {

             var load = '<h1>Please Wait</h1>' +
                 '<i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i> <span>Loading ...</span>';
             $.mobile.loading('show', {
                 text: 'Please wait...',
                 textVisible: true,
                 html: load,
                 theme: 'z'
             });
         },
         function error(err) {

         },
         function complete() {
             $.mobile.loading('hide');
             var uni = localStorage.getItem("lunicode");
             var ref = firebase.database().ref(uni);
             var myname = storage.getItem("uname");
             var myid = storage.getItem("myid");
             var downloadURL = task.snapshot.downloadURL;
             ref.push({
                 name: myname,
                 imageurl: downloadURL,
                 photoUrl: "/images/profile_placeholder.png",
                 my_id: myid,
             });
         }
     );

 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以将图像从 img 标签上传到 firebase 存储吗 - Can I upload image to firebase storage from img tag 我正在尝试将图像上传到 firebase 存储并获取下载 url - I'm trying to upload an image to firebase storage and get the download url 当我上传图像 firebase 存储 getdownloadUrl() 将下载 url 放入 Firestore Collections 但未在 React JS 中设置 - When I upload a image firebase storage getdownloadUrl() put download url In Firestore Collections but not set it in React JS 成功将图像上传到 Firebase 存储后,Firebase 获取下载 URL - Firebase get Download URL after successful image upload to firebase storage 如何将图像上传到 firebase 存储,然后返回下载 url? - How to upload an image to firebase storage and then return the download url? 如何将文件上传到 Firebase 存储并显示下载 URL? - how can i upload file to firebase storage and display the download URL? azure存储上传图片文件并获取其url以img标签显示 - azure storage upload image file and get its url to display with img tag 在 React.js 中使用 Dropzone 时如何将图像上传到 Firebase 存储并获取图像的下载 URL - How to Upload Images to Firebase Storage and Get Image's Download URL's When Using Dropzone in React.js 再次上传时,图像不显示在 img 标签中 - image is not show in img tag when upload it again 检索 Firebase 存储图像的下载 url 时出现问题 - Problem retrieving download url of Firebase storage image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM