gpt4 book ai didi

asp.net - 在 asp.net 中将 Html2canvas 图像保存到服务器

转载 作者:行者123 更新时间:2023-12-05 06:43:24 25 4
gpt4 key购买 nike

我想将 HTM2Canvas 图像保存到服务器。尝试了一些代码但无法将图像保存到目录。我想将屏幕截图保存在目录中,并想将路径保存到数据库。

我已经发布了我的代码,在这个例子中我只想将图像保存在目录中。但无法做到这一点。请帮我解决这个问题。

    <form id="myForm" method="post" action="demo.aspx"  runat="server">
<div id="target">

<div id="wrapper">
<div id="header">
<div class="text">
<Span>This Space for rent/price</Span>
<h1>This Space for Property
Address</h1>
<h1>This Space for Data</h1>
</div>
</div>
<h1 style="text-transform:uppercase;">This Space For advert heading</h1>


</div>

</div>
<asp:Button ID="btnSave" runat="server" Text="Save As Image" OnClientClick="return PrintDiv();" />

<script src="js/html2canvas.js"></script>
<script type="text/javascript">
function PrintDiv() {
html2canvas(document.body, {
onrendered: function (canvas) {
var img = canvas.toDataURL().replace(/^data[:]image\/(png|jpg|jpeqg)[;]base64,/i, "");
alert(img);
$.ajax({
type: "POST",
url: "demo.aspx/UploadImage",
data: {"imageData": name },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
});

}
});
function OnSuccess(response) {
alert(response.d);
}
}
</script>

</form>
</body>
</html>


ASP.NET Code behind File:


[WebMethod()]

public static void UploadImage(string imageData)
{
string fileNameWitPath = "~/Client/ScreenShot/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();
}
}
}

最佳答案

try this for javascript

html2canvas(document.body, {
onrendered: function (canvas) {
var image = canvas.toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
var param = { imageData: image };
$.ajax({
url: "demo.aspx/UploadImage",
data: JSON.stringify(param),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert('Image saved successfully !');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
var err = eval("(" + XMLHttpRequest.responseText + ")");
}
});
}
});

try this for code behind

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static void UploadImage(string imageData)
{
try
{
string fileNameWitPath = "~/Client/ScreenShot/custom_name.png";
using (FileStream fs = new FileStream(fileNameWithPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
}
catch (Exception ex)
{

}
}

关于asp.net - 在 asp.net 中将 Html2canvas 图像保存到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33625022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com