当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to save html2canvas image to my project images folder?

问题描述:

I am not sure how to specify the path for the file to be save (like ../images/screenshot.png)I tried saving the image like

html2canvas($my-div, {

useCORS: true,

allowTaint: true,

onrendered: function (canvas) {

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

location.href = img;

}

});

This gets downloaded to my system downloads.How do I save this to the folder inside the project (want to specify the path I want to save the file)?

网友答案:

You have to use some library like Canvas2Image to download image. User will specify image path by it self (you don't know where to save it, because there may be no such directory as 'C:\Users{SomeUsername}\Downloads'

html2canvas($my-div, {
    onrendered: function(canvas) {
        return Canvas2Image.saveAsPNG(canvas);
    }
});
网友答案:

Finally solved this problem. The plugin that helped me to save canvas to image in my local storage is canvas2image plugin from devgeeks. Directly pass your canvas as parameter to the save function of plugin which will save that canvas as image to your local storage.

    html2canvas($my-div, {
    height: myDivHeight,
    width: 350,
    useCORS: true,
    allowTaint: true,
    proxy: "your url",
    onrendered: function (canvas) {
      window.canvas2ImagePlugin.saveImageDataToLibrary(
        function(msg){
          console.log(msg) //path where image is saved
        },
        function(err){
            console.log(err);
        },
        canvas // pass canvas as third parameter
      );
    }
  });

I used html2canvas library to captured the current screen and used canvas2Image plugin to save it to my local storage. Cheers!!!

分享给朋友:
您可能感兴趣的文章:
随机阅读: