Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a base64-encoded image?

I have a base64-encoded image from the server for which I want to force the download through JavaScript. Is is possible?

like image 747
Mario Avatar asked Dec 23 '12 12:12

Mario


People also ask

How do I get an image from base64 to URL?

One way to convert an image file into a base64 string is to put it into a canvas. Then we can call the canvas's toDataURL method to convert it into a base64 string. We create the getBase64Image function that takes the url string for the URL of the image. Then we create an img eklement with the Image constructor.

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.


1 Answers

  1. If you want to download it using JavaScript (without any back-end) use:

    window.location.href = 'data:application/octet-stream;base64,' + img; 

    where img is your base64 encoded image.

  2. If you want to allow the user to specify a file name, use the download attribute of the a tag:

    <a download="FILENAME.EXT" href="data:image/png;base64,asdasd...">Download</a> 
    • Notice: The download attribute is not supported by very old browsers
like image 172
Minko Gechev Avatar answered Oct 21 '22 23:10

Minko Gechev