I am retrieving a Blob image from a database, and I'd like to be able to view that image using javascript. The following code produces a broken image icon on the page:
var image = document.createElement('image');
image.src = 'data:image/bmp;base64,'+Base64.encode(blob);
document.body.appendChild(image);
Here is a jsFiddle containing all the code required, including the blob. The completed code should properly display an image.
Thank you for your help.
You can also get BLOB object directly from XMLHttpRequest. Setting responseType to blob makes the trick. Here is my code:
And the response function looks like this:
We just have to make an empty image element in HTML:
In your example, you should
createElement('img')
.In your link,
base64blob != Base64.encode(blob)
.This works, as long as your data is valid http://jsfiddle.net/SXFwP/ (I didn't have any BMP images so I had to use PNG).
I guess you had an error in the inline code of your image. Try this :
Helpful link :http://dean.edwards.name/my/base64-ie.html
If you want to use fetch instead:
Please follow this link : https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
You can convert your string into a Uint8Array to get the raw data. Then create a Blob for that data and pass to URL.createObjectURL(blob) to convert the Blob into a URL that you pass to img.src.
You can try the complete example at: http://jsfiddle.net/nj82y73d/
The problem was that I had hexadecimal data that needed to be converted to binary before being base64encoded.
in PHP: