2

I am creating a chart in partial div on pageload. I am converting that into an image using html2canvas lib. That image should get saved to the application's directory.

Using canvas2image lib I want to download and save that image to disk. How should I pass the url?

Charlie
  • 22,886
  • 11
  • 59
  • 90
Priya
  • 61
  • 4
  • 1
    I think this is what you're looking for http://stackoverflow.com/questions/7951326/save-image-to-users-disk-using-javascript – Jonas Grumann Oct 18 '16 at 08:05

1 Answers1

0

html2canvas library supplies a canvas as an argument to the onrendered function. You can use toDataURL of the canvas to get the image data in base64 format.

html2canvas($parentNode, {
                                    width: 300,
                                    height:300,
                                    onrendered: function(canvas) {                                        
                                        var myData = canvas.toDataURL(); 
                                    }               
                           });

Now link this data with an anchor element to make it downloadable as done in this question.

Community
  • 1
  • 1
Charlie
  • 22,886
  • 11
  • 59
  • 90
  • OP is using [canvas2image](https://github.com/hongru/canvas2image) library which takes a canvas element as input. What he wants is not implemented in this library, which almost just does what you are doing btw... – Kaiido Oct 18 '16 at 08:34
  • Hi Charlie, thanks for the reply. but the thing is I am not using any link to download it, it should get downloaded and saved internally on page load, as I am using that image in PDF download latter. I used the above solution earlier. but that is not the way it should get performed. – Priya Oct 18 '16 at 09:27
  • There is no way you can save an image to disk without telling the user. The closest you can get is to have a popup asking where he would like to save it. If you need the image on your site later on, I think you should post it to the server, store it and load it from there later. – Jonas Grumann Oct 18 '16 at 09:30
  • There is no user activity involved in this functionality. I think you are right. I will do it from server side only. Thanks Jonas Grumann – Priya Oct 18 '16 at 09:46
  • OR you can save it to the LocalStorage or SessionStorage instead of the server. But this is only few MBs wide. – Charlie Oct 18 '16 at 13:16