Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving canvas locally in IE

Hi I want to save a canvas locally in IE.

  var img = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");

I couldn't manage to download it with following ways.

1) document.execCommand("SaveAs"..
2) window.location.href = img;
3) $.fileDownload(img);  // jquery download file library-
4) canvas2image // cross domain problem.

Is there a way to save canvas locally in IE without base64 or cross domain problem? Thank you very much.

like image 985
user1874941 Avatar asked Apr 10 '13 16:04

user1874941


People also ask

How do I get canvas toDataURL?

Here is the code to do that: var canvas = document. getElementById("ex1"); var dataUrl = canvas. toDataURL(); window.

How do I save an image as a canvas in Javascript?

You can save a canvas to an image file by using the method canvas. toDataURL() , that returns the data URI for the canvas' image data. The method can take two optional parameters canvas.


1 Answers

I know this is late, but I stumbled on this question in my search to do the same thing and I wanted to share a solution that's working for me.

Assuming you have a data URI already, you can create a blob and then use msSaveBlob or msSaveOrOpenBlob

Example:

dataURItoBlob = function(dataURI) {
        var binary = atob(dataURI.split(',')[1]);
        var array = [];
        for(var i = 0; i < binary.length; i++) {
            array.push(binary.charCodeAt(i));
        }
        return new Blob([new Uint8Array(array)], {type: 'image/png'});
    }

var blob = dataURItoBlob(uri);
window.navigator.msSaveOrOpenBlob(blob, "my-image.png");

I used this answer for a bulk of my solution.

After typing this, I realize this doesn't really help you with the cross domain issue, so it's a partial answer at best. With that, all I can say is consider using data URIs where possible, if cross domain is an issue.

like image 72
Yetti Avatar answered Sep 24 '22 16:09

Yetti