Javascript 在 html img 标签中显示来自 firebase 存储的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38452633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:30:43  来源:igfitidea点击:

Display images from firebase storage in html img tags

javascripthtmlfirebasefirebase-storage

提问by crispy2k12

I'm attempting to display an image from firebase into html imgtags, but it fails to retrieve the image.

我正在尝试将 firebase 中的图像显示为 htmlimg标签,但无法检索图像。

Javascript code:

Javascript代码:

var storageRef = firebase.storage().ref();
var spaceRef = storageRef.child('images/photo_1.png');
var path = spaceRef.fullPath;
var gsReference = storage.refFromURL('gs://test.appspot.com')

storageRef.child('images/photo_1.png').getDownloadURL().then(function(url) {
  var test = url;
}).catch(function(error) {

});

html code:

html代码:

<img src="test" height="125" width="50"/>

采纳答案by Mosh Feu

Once you have the testvariable, you need to set the image's src to it using a script.

获得test变量后,您需要使用脚本将图像的 src 设置为它。

Something like this:

像这样的东西:

//var storage    = firebase.storage();
//var storageRef = storage.ref();
//var spaceRef = storageRef.child('images/photo_1.png');
//
//storageRef.child('images/photo_1.png').getDownloadURL().then(function(url) {
//
//
//  var test = url;
//  add this line here:
//  document.querySelector('img').src = test;
//
//}).catch(function(error) {
//
//});
//
var test = 'firebase_url';

document.querySelector('img').src = test;
<img height="125" width="50"/>

回答by VyTcdc

The bellow two lines which is commented are not required, I have tested. it is working fine.

下面两行注释是不需要的,我已经测试过了。它工作正常。

//var path = spaceRef.fullPath;
//var gsReference = storage.refFromURL('gs://test.appspot.com')

    <script>
     function showimage() {



         var storageRef = firebase.storage().ref();
         var spaceRef = storageRef.child('sweet_gift/vytcdc.png');
         storageRef.child('sweet_gift/vytcdc.png').getDownloadURL().then(function(url) {
             var test = url;
             alert(url);
             document.querySelector('img').src = test;

         }).catch(function(error) {

         });


     }
    </script>
    <input type="button" value ="view Image" id="viewbtn" onclick="showimage();">
    <img src="test" height="125px" width="200px"/> 

回答by Thando Zondo

try this :-)

尝试这个 :-)

//var storage    = firebase.storage();
//var storageRef = storage.ref();
//var spaceRef = storageRef.child('images/photo_1.png');
//
//storageRef.child('images/photo_1.png').getDownloadURL().then(function(url) {
//
//
//  var test = url;
//  add this line here:
//  document.getElementById("your_img_id").src = test;
//
//}).catch(function(error) {
//
//});
//

<img height="125" width="50" id="your_img_id" src=""/>

回答by Rajender rathore

           var uploader=document.getElementById('uploader'),
           fileButton=document.getElementById('fileButton');

           fileButton.addEventListener('change', function(e) {

           var file=e.target.files[0];

           var storageRef=firebase.storage().ref("'/images/'"+file.name);


            var task=storageRef.put(file);

            task.on('state_changed',

            function progress(snapshot){
            var percentage=( snapshot.bytesTransferred / snapshot.totalBytes )*100;
            uploader.value=percentage;
            if (percentage==100){
            alert("file uploaded Successfully");
            }
            },
            function error(err){

            },
            function complete(){

                 var text1=document.getElementById('text3');
                 var text7=document.getElementById('text4');
                 var text8=document.getElementById('text5');
                 var text9=document.getElementById('text6');

                var downloadURL =task.snapshot.downloadURL; 
                var postkey=firebase.database().ref('data-modeling/').push();
                var text2=text1.value;
                postkey.child("Name").set(text2);
                var texta=text7.value;
                postkey.child("Address").set(texta);
                var textb=text8.value;
                postkey.child("Age").set(textb);
                var textc=text9.value;
                postkey.child("PhoneNo").set(textc);
                postkey.child("url").set(downloadURL)
                alert('successful Submit');




             });

            }


        );

回答by sangram desai

This is a well-tested code. it is working fine. just follow these steps.

这是一个经过良好测试的代码。它工作正常。只需按照以下步骤操作即可。

html code:

html代码:

<img id="myImgId" src="" height="125" width="50"/>

Add config Information here

在此处添加配置信息

  con = {
        "apiKey": "your key",
        "authDomain": "example.firebaseapp.com",
        "databaseURL": "https://example.firebaseio.com/",
        "projectId": "example",
        "storageBucket": "example.appspot.com",
        "messagingSenderId": "id"
    };

Initialize firebase using this

使用这个初始化firebase

firebase.initializeApp(con);

Create a reference with an initial file path and name

创建具有初始文件路径和名称的引用

var storage = firebase.storage();
var storageRef = storage.ref();
//urll is the url for image
storageRef.child(urll).getDownloadURL().then(function(url) {
  // Or inserted into an <img> element:
  var img = document.getElementById('myImgId');
  img.src = url;
}).catch(function(error) {
  // Handle any errors
});