0

I am new to JavaScript Objects and i got a list of object and in that object there is a property called signature which is BLOB, I am trying to decode it so I can display it as image, but when I use it, I am unable to use it.upon debugging in console, the returned item is a number but it has following structure

"{
  "signature": "/9j/4AA"

but when I do console.log(typeof + deca); it says number, following is my code

    for (const sign of result){
        const deca = atob(sign.signature);
        console.log(typeof + deca);

console.log(deca) I get this

and using it to display like

            <img *ngFor="let sign of selectedUser"
                 [src]="sign.imageData"
                 alt="">

Do I need to use trim or slice to cut off the "{ "signature": part or I am doing something wrong?

localhost
  • 822
  • 2
  • 20
  • 51

1 Answers1

2

this happens because of the + operator, which automatically makes the variable a number.

https://jsfiddle.net/n97ssLma/

var test = "test"
alert(typeof test) //string
alert(typeof + test) //number

to display your blob as an image do the following:

var image = document.createElement('img');
    
image.src="data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==";
    
image.width=100;
image.height=100;
image.alt="here should be some image";
    
document.body.appendChild(image);

Using Javascript to Display Blob

updated your fiddle: https://jsfiddle.net/jt5ks76z/1/

example using atob method:

https://jsfiddle.net/jt5ks76z/4/

messerbill
  • 5,499
  • 1
  • 27
  • 38
  • Thanks, but my question main part was why do I get signature (object property when I do `atob`, it should just return me a base64 decode, – localhost Feb 02 '18 at 15:44
  • I added when I do `console.log(deca)` what do I get. I am using angular. – localhost Feb 02 '18 at 15:49
  • in this case please update your question with the code of your TypeScript Class of the relevant component – messerbill Feb 02 '18 at 15:51
  • It is looping throught a object, and it has property signature.Have a look at my question again/ I explained what is doing on. – localhost Feb 02 '18 at 15:57
  • i think you do not get what i said....or i don't get what you are saying. Have a look here: https://jsfiddle.net/jt5ks76z/1/ – messerbill Feb 02 '18 at 16:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164416/discussion-between-nofel-and-messerbill). – localhost Feb 02 '18 at 16:31