Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Javascript code:

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:

<img src="test" height="125" width="50"/>
like image 456
crispy2k12 Avatar asked Jul 19 '16 07:07

crispy2k12


People also ask

How can I get image URL from Firebase storage web?

Image URL is obtained by uploading an image to firebase bucket and then that can return back a URL that URL is a permanent URL which can be open anywhere. Then a user can use this URL for any purpose in its application.

Can I use firebase to store images?

Firebase provides secure file uploads and downloads for Firebase application. This article explains how to build an Android application with the ability to select the image from the mobile gallery and upload images to Firebase Storage.


2 Answers

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"/> 
like image 73
VyTcdc Avatar answered Sep 17 '22 15:09

VyTcdc


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

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"/>
like image 40
Mosh Feu Avatar answered Sep 19 '22 15:09

Mosh Feu