[英]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.