简体   繁体   English

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

[英]Display images from firebase storage in html img tags

I'm attempting to display an image from firebase into html img tags, but it fails to retrieve the image.我正在尝试将 firebase 中的图像显示为 html img标签,但无法检索图像。

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"/>

Once you have the test variable, 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"/>

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"/> 

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=""/>

This is a neat way to load the images if you know the filenames in advance:如果您事先知道文件名,这是加载图像的一种巧妙方法:

https://firebasestorage.googleapis.com/v0/b/{{BUCKET}}/o/images%2{{FILENAME}}?alt=media

Remove the ?alt=media to get some metadata.删除 ?alt=media 以获取一些元数据。

           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');




             });

            }


        );

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
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM