我正在使用 html2canvas 库从页面获取快照,以及如何在我的 C# Controller 中获取该图像?
查看
$("#button").click(function () {
var image;
html2canvas(document.body, {
onrendered: function (canvas) {
image = convertCanvasToImage(canvas);
$.post("../../HomeController/SetScreenShot", {
sendImage: image
}, function () {});
}
});
});
Controller
public void SetScreenShot()
{
HttpPostedFileBase file = new HttpPostedFileBase();
file = Request.Form["sendImage"].ToString();
}
最佳答案
简单来说,隐藏的输入可以被跳过。这就能解决问题:
$("#button").click(function () {
html2canvas(document.body, {
onrendered: function(canvas) {
var image = canvas.toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
$.ajax({
type: "POST",
url: '../../HomeController/SaveSnapshot',
dataType: 'text',
data: { base64data : image },
success: function(result) { alert(result); }
});
}
});
CONTROLLER (修改版本,将图像保存到映射文件夹):
[HttpPost]
public ActionResult SaveSnapshot()
{
bool saved = false;
if (Request.Form["base64data"] != null) {
string image = Request.Form["base64data"].ToString();
byte[] data = Convert.FromBase64String(image);
var path = Path.Combine(Server.MapPath("~/Upload"), "snapshot.png");
System.IO.File.WriteAllBytes(path, data);
saved = true;
}
return Json(saved ? "image saved" : "image not saved");
}
关于c# - 如何将屏幕截图发送到 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23220723/