0

I have a request to an url that retrieves an image, it's something like this:

this.$http.get(url, { }).success( (data) => { 
    console.log(data)
});

The result of console.log(data) is somethis like this

�PNG ���IHDR����������SZ���sRGB�������gAMA�����a���pHYs�������o�d���IDATx^�t�ֵ=��3333339qb...

So, how can I convert this result to a base64 string ?

mmarques
  • 625
  • 3
  • 9
  • 27
  • Uhm, are you making an ajax request to get a binary file ? – adeneo May 26 '15 at 15:11
  • Yes. I need to store the image base64 string content – mmarques May 26 '15 at 16:02
  • If it where me, I'd create a serverside script that gets the image and converts it, that way you circumvent the same-origin policy if that's ever an issue, and serve up Base64 from the server. – adeneo May 26 '15 at 16:06

1 Answers1

0

You can use btoa method:

console.log(window.btoa(data))
antyrat
  • 27,479
  • 9
  • 75
  • 76
  • I think i cant achieve my goal with this method. I made a test setting img.src = "data:image/png;base64," + window.btoa(data) and I didn't get the actual image :/ – mmarques May 26 '15 at 16:14
  • @mmarques can you give me window.btoa(data) console output? Or maybe you you have some example to show us? – antyrat May 26 '15 at 16:15
  • The output is quite long: 77+9UE5HDQoaCgAAAA1JSERSAAABYAAAAO+/vQgGAAAA77+9U1oLAAAAAXNSR0IA77+977+9HO+/vQAAAARnQU1BAADvv73vv70L77+9YQUAAAAJcEhZcwAADu+/vQAADu+/vQHvv71v77+9ZAAA77+977+9SURBVHhe77+977+9B1gc77+977+977+977+977+977+9XXrvv73vv70IVO+/vR0BEu+/vR01JCTUkO+/vUDvv70oAu+/vToI77+9Xu+/vV5t77+9yb1377+9dmI7du+/ve+/ve+/vXEc77+9Ne+/vVPvv73vv73vv71577+9fO+/ve+/vX8/Z3YB77+977+9ee+/ve+/ve+/ve+/ve+/ve+/vV7vv71977+977+977+977+9bDvvv73vv71777+977+977+977+9aO+/ve+/vQDvv71B77+977+977+9CA8L77+9Su+ ...... – mmarques May 26 '15 at 16:21
  • @mmarques maybe [this](http://stackoverflow.com/questions/8022425/getting-blob-data-from-xhr-request) answer will help you – antyrat May 26 '15 at 16:26