如何在jquery中将html2canvas图像保存到系统文件夹中

时间:2014-04-07 08:31:06

标签: c# jquery asp.net html2canvas

我有一个id =" form1"在这个表单里面我有一个图。现在我使用html2canvas来获取这个form1的图像。这是我的代码..

<script type="text/javascript">

      $(document).ready(function () {
          $('#add_button').click(function () {
              alert("hiii");
              $('form1').html2canvas();
              var queue = html2canvas.Parse();
              var canvas = html2canvas.Renderer(queue, { elements: { length: 1} });
              var img = canvas.toDataURL();
              window.open(img);
              alert("Hello");
          });
      });

  </script>

<form id="form1" runat="server">
<div style="padding-left:150px">
  <asp:Literal ID="FCLiteral1" runat="server"></asp:Literal>
</div>
<div style="padding-left:350px"><b>Demo</b></div>
</form>

 <input type="submit" id="add_button" value="Take Screenshot Of Div" " />

所以我的问题是如何将此图像保存到我的系统硬盘中。请帮助我。

2 个答案:

答案 0 :(得分:8)

系统硬盘?我不明白,服务器还是客户端?

客户端

如果您希望用户自动下载图像,则需要修改Data URI scheme

试试这个:

添加css

#myHideFrame {
    position: absolute;
    top: -9999px;
    width: 1px;
    height: 1px;
}

添加Javascript

var img = canvas.toDataURL();
var frame = document.getElementById("myHideFrame");
if(!frame) {
    frame = document.createElement("iframe");
    frame.id = "myHideFrame";
    document.body.appendChild(frame);
}
frame.src = img.replace(/^data[:]image\/(png|jpg|jpeg)[;]/i, "data:application/octet-stream;");

不幸的是这个例子没有显示名称,为此你必须做这样的事情(用户需要点击链接):

var img = canvas.toDataURL();
var link = document.createElement("a");
link.download = "photo.png"; //Setup name file
link.href = img.replace(/^data[:]image\/(png|jpg|jpeg)[;]/i, "data:application/octet-stream;");
document.body.appendChild(link);

SERVER

如果要保存在服务器上,则需要使用Ajax,例如Jquery:

Javascript文件:

var img = canvas.toDataURL().replace(/^data[:]image\/(png|jpg|jpeg)[;]base64,/i, "");
$.ajax({
    "type": "POST",
    "url": "upload.aspx/UploadImage",
    "data": { 
        "imageData": img //Send to WebMethod
    }
}).done(function(o) {
    console.log(["Response:" , o]); 
});

您的upload.aspx.cs文件需要:

...
[WebMethod()]
public static void UploadImage(string imageData)
{
    string fileNameWitPath = "custom_name.png";
    using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
    {
        using (BinaryWriter bw = new BinaryWriter(fs))
        {
            byte[] data = Convert.FromBase64String(imageData);//convert from base64
            bw.Write(data);
            bw.Close();
        }
    }
}
...

查看详情:http://www.dotnetfunda.com/articles/show/1662/saving-html-5-canvas-as-image-on-the-server-using-aspnet

答案 1 :(得分:1)

客户端方面保存图片的更简单的解决方案是生成图像数据并将其附加到<a>标记,并附加download属性。

以下是我的例子:

<强> HTML:

<a href="#" class="downloadAsImage hide" download>Download</a>

<强> JS:

$(function() {
    html2canvas($('.main'), {
        onrendered: function(canvas) {
            $('.downloadAsImage').attr('href', canvas.toDataURL()).removeClass('hide');
        }
    });
});

旁注:请注意,您无法通过JS点击$('.downloadAsImage'),因为它具有download属性。