简体   繁体   English

用户将div捕获为图像并保存到计算机

[英]user capture div as image and save to computer

I have a Div on my site, I want to place a button/link (or other things of the sort) that when clicked will save the div and all its contents to the users computer, much like the printing code which is used to print divs. 我在我的网站上有一个Div,我想放置一个按钮/链接(或其他类似的东西),当点击它时会将div及其所有内容保存到用户计算机,就像用于打印的打印代码一样div的。 I'm a coding novice so all help will be apreciated. 我是一个编码新手所以所有的帮助都会被贬低。

There is a browser support limit doing this. 这样做有一个浏览器支持限制。 HTML2Canvas can render your HTML content into a canvas element. HTML2Canvas可以将HTML内容呈现为canvas元素。 Then you can use canvas.toDataURL("image/png"); 然后你可以使用canvas.toDataURL("image/png"); ( docs in MDN ) method to export the canvas element to an jpeg or png image. MDN中的文档 )方法将canvas元素导出为jpegpng图像。

It's not widely supported but it's still possible. 它没有得到广泛的支持,但仍有可能。

Easy way 简单的方法

 var MyDiv1 = document.getElementById('DIV1');
 var MyDiv3 = document.getElementById('DIV2');
 MyDiv3.innerHTML = MyDiv1.innerHTML;


 html2canvas(MyDiv3, {
                    useCORS: true,
                    onrendered: function (canvas) {
                    var dataUrl = canvas.toDataURL("image/png");
                    document.getElementById("HiddenField1").value = dataUrl;

                    }
                });

use a button and call ur hidden field value 使用按钮并调用您隐藏的字段值

ButtonClick1()
{
    string imgval = HiddenField1.Value;
    imgval = imgval.Replace("data:image/png;base64,", "");
    byte[] imgData = Convert.FromBase64String(imgval);

    using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(imgData)))
    {
        String path = Server.MapPath("~/imgFolder");
         image.Save(path + "\\output.jpg", ImageFormat.Jpeg);  // Or Png

    }
}

An easy way to do this would be to use GrabzIt's JavaScript API to capture a div . 一个简单的方法是使用GrabzIt的JavaScript API来捕获div You would just need to do something like the following. 您只需要执行以下操作即可。 Where #features is the id of the div you want to capture. 其中#features是您要捕获的div的id。

<script type="text/javascript" src="grabzit.min.js"></script>
<script type="text/javascript">
GrabzIt("Your Application Key").ConvertURL("http://www.example.com/my-page.html",
{"target": "#features", "bheight": -1, "height": -1, "width": -1}).Create();
</script>

Disclaimer I built this API! 免责声明我建立了这个API!

Assuming you want a text or HTML file, not an image file like a screen shot, JavaScript by itself can't initiate a "Save" dialog on a web browser, only a response from a web request to a server will do so. 假设你想要一个文本或HTML文件,而不是像屏幕截图那样的图像文件,JavaScript本身不能在Web浏览器上启动“保存”对话框,只有来自Web服务器请求的响应才会这样做。

To start with, you'll need a form with your button and a hidden field: 首先,您需要一个包含按钮和隐藏字段的表单:

<div id="saveme">stuff to save</div>
<form action="saveme.aspx" method="POST">
  <input type="submit" onclick="prepsave()">
  <input type="hidden" id="savemepost">
</form>

And you need some Javascript to save the DIV contents to the hidden field before submittal: 在提交之前,您需要一些Javascript将DIV内容保存到隐藏字段:

<script>
   function prepsave() {
      document.getElementById("savemepost").value = 
        document.getElementById("saveme").innerHTML;
        return true;
   }
</script>

On the server, you'll need some code to accept the text and spit it back out in the form of a file attachment: 在服务器上,您需要一些代码来接受文本并以文件附件的形式将其吐出:

<script runat="server">
   Response.Clear()
   Response.AddHeader("content-disposition","attachment; filename=saved.html")
   Response.Write(Request.Form("savemepost"))
</script>

Caveat #1: There are probably some minor bugs in there and plenty of room for improvement, this is just a proof of concept. 警告#1:可能存在一些小错误,还有很大的改进空间,这只是一个概念证明。

Caveat #2: The server-side code above is potentially insecure, as it allows any web page to control content going to the user's web browser from your domain. 警告#2:上面的服务器端代码可能不安全,因为它允许任何网页控制从您的域进入用户的Web浏览器的内容。 You'll need to add some measures to protect this from being abused. 您需要添加一些措施来防止这种情况被滥用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM