8

To start with JS isn’t one of my strengths. I’ve been trying for the past couple of days to edit this JS function to make it force download base64 image. What the function does, when the download button is clicked, is open a new window with the image on it. The user then has to right click and save the picture. I’m trying to force download the image instead of right click and ‘save as’.

The dataurl produce base4 png string (data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQ………)

i tried using as it was suggested in another thread but that didn't work.

All suggestions are welcomed. Thanks.

savePaint: function() {
            var self = this;

            dataURL = self.context.canvas.toDataURL();

            var cntnt = $("<p class='dialogHeader'>Please right click and select 'Save Image As' option. Click here to Return</p>           <img id='PrintImage' src=" + dataURL + ">");
            self.newSavedImage.html(cntnt);

            self.showPopup(self.newSavedImage, self.canvasWidth, self.canvasHeight)
}
user2334436
  • 949
  • 5
  • 13
  • 34
  • Does this answer your question? [How to download a base64-encoded image?](https://stackoverflow.com/questions/14011021/how-to-download-a-base64-encoded-image) – ggorlen Jul 28 '22 at 02:50

3 Answers3

4

Finally! I got it to work. For anyone facing the same problem I found a function here http://danml.com/download.html that will let you force download the base64.

user2334436
  • 949
  • 5
  • 13
  • 34
  • 10
    Link-only answers are no good. Please bring over the relevant code into your answer. – random_user_name Apr 22 '16 at 19:55
  • 3
    thumbs down til relevent code added. the link is too much a wall of text. better simple answer with code here: http://stackoverflow.com/questions/14011021/how-to-download-a-base64-encoded-image – Andrew May 09 '17 at 20:27
1

You could specify a different MIME type rather than image/jpeg so the browser doesn't attempt to open the image natively:

data:application/octet-stream;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/...

This will force the file to download, but I noticed in some browser configurations may autosave with a default filename and since the browser doesn't know the real file type it will be up to the user to specify the correct type. Unfortunately the data URI scheme does not allow for a suggested file name.

Check out Using HTML5/Javascript to generate and save a file for more possible solutions. Your best bet, depending on your requirements, might be to go with a Flash-based solution such as Downloadify.

JasonCG
  • 889
  • 13
  • 21
1

you can try this:
javascript file:

var valuToDecode = document.getElementById("base64-image-string").value;
var linkImg = valuToDecode.concat('" alt="Red dot" download="ganixo-file.png"> click here to download your img </a>');
document.getElementById("codedImg").innerHTML=  '<a href="data:image/jpeg;base64,'.concat(linkImg);

html file:

Image url:<p  id="codedImg"></p>

output:

<p id="codeImg"><a href="data:image/jpeg;base64,iVB..." download="img.png">click here to download your img</a></p>