Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display base64 encoded image in HTML if it is located in a separated file?

I have base64 encoded image. If I put it right into html it works:

<img src="data:image/png;base64,..."/>

But when I put all that base64 content into a separated file, it doesn't:

<img src="image.base64.txt"/>

I tried changing extension to .png, but it doesn't help. Any ideas?

like image 912
serg Avatar asked Oct 12 '10 17:10

serg


People also ask

How do I view Base64 encoded images?

Images encoded with Base64 can be embedded in HTML by using the <img> tag. This can help to increase the page load time for smaller images by saving the browser from making additional HTTP requests.

How do I display Base64 in HTML?

The <img> tag has a src attribute and contains the Data URL of the image. A Data URL is composed of two parts, which are separated by a comma. The first part specifies a Base64 encoded image, and the second part specifies the Base64 encoded string of the image. Add also an alt attribute.

Where are Base64 images stored?

Instead, they are stored in their raw binary form in a binary column, blob column, or file. Base64 is only used as a transport mechanism, not for storage. For example, you can embed a base64 encoded image into an XML document or an email message. Base64 is also stream friendly.


1 Answers

You would need to send the correct Content-type, Content-encoding and charset HTTP headers along with the file. Note that they are all part of the data: URI schema as well. You really should have a charset=utf-8 or similar clause between the content-type and the encoding:

url(data:image/png;charset=utf-8;base64,...);
like image 59
Stan Rogers Avatar answered Oct 15 '22 05:10

Stan Rogers