Loopback-storage-component how to display image file

I use loopback-component-storage to upload images to the server. https://docs.strongloop.com/display/public/LB/Storage+component

I also have an SDK for AngularJS JavaScript, so my lb-services.js is dynamically updated. https://docs.strongloop.com/display/public/LB/AngularJS+JavaScript+SDK

Now I want to display on my web page a photo from this loopback component storage model called a container, so I can use the angular getFile (from lb-service) in my controller method, which is "Get information for the specified file within the specified container". eg:

$scope smthing = Container.getFile({container: 'news-imgs', file: 'smallpic.pnh'})

or I can use the upload method, which "uploads the file to the specified container". eg:

$scope smthing = Container.download({container: 'news-imgs', file: 'smallpic.png'})

in my controller.

Now the problem is that when I write in the {{smthing}} .html file, the first method will show me

{ "container": "news-imgs", "name": "smallpic.png", "size": 757, "atime": "2015-12-01T11:49:36.840Z", "mtime": "2015-12-01T11:49:36.840Z", "ctime": "2015-12-01T11:49:36.840Z" }

and the second method will show as many sad signs as this:

"0":" ","1":"P","2":"N","3":"G","4":"\r","5":"\n","6":"\u001a","7":"\n","8":"\u0000","9":"\u0000","10":"\u0000","11":"\r","12":"I","13":"H","14":"D","15":"R","16":"\u0000","17":"\u0000","18":"\u0000","19":"\u0014","20":"\u0000","21":"\u0000","22":"\u0000","23":"\u0012","24":"\b","25":"\u0003","26":"\u0000","27":"\u0000","28":"\u0000","29":"l","30":"\u000e","31":"\u000e","32":"\"","33":"\u0000","34":"\u0000","35":"\u0000","36":"\u0019","37":"t","38":"E","39":"X","40":"t","41":"S","42":"o","43":"f","44":"t","45":"w","46":"a","47":"r","48":"e","49":"\u0000","50":"A","51":"d","52":"o","53":"b","54":"e","55":" ","56":"I","57":"m","58":"a","59":"g","60":"e","61":"R","62":"e","63":"a","64":"d","65":"y","66":"q","67":" ","68":"e","69":"<","70":"\u0000","71":"\u0000","72":"\u0001","73":" ","74":"P","75":"L","76":"T","77":"E","78":" ","79":" ","80":" ","81":" ","82":" ","83":" ","84":" ","85":"\u0000","86":"r","87":" ","88":" ","89":" ","90":" ","91":"\u0000","92":" ","93":" ","94":"\u0000","95":"~","96":" ","97":" ","98":" ","99":" ","100":"\u0000","101":"x","102":" ","103":"\u0012","104":" ","105":" ","106":"\u0000","107":"","108":" ","109":"\t","110":" ","111":" ","112":"\u0016","113":"{","114":" ","115":"\u0002","116":" ","117":" ","118":"(",...etc

I think this is an Image object, but in json, right?

I also tried to throw the {{smthing}} tag in <img src=""> and <img ng-src=""> , but it doesn’t work either.

Question: How to display this photo? Can someone help me please?

Hi Alan.

+5
source share
1 answer

No need to convert to base64 to display an image. Just use this trick.

Suppose your container Name and Image Name

  • Container Name = news-imgs
  • File Name = smallpic.png

    Then the download link will look like

    /api/containers/news-imgs/download/smallpic.png

    This means that you can prepare your download link for

    /api/container/Your-Container-Name/download/Your-File-Name

And in your view you can show an image like

 <img ng-src="/api/container/Your-Container-Name/download/Your-File-Name" /> <img ng-src="/api/containers/news-imgs/download/smallpic.png" /> 

Now use Angular to dynamically create download links for each image.

+2
source

Source: https://habr.com/ru/post/1237170/


All Articles