我正在尝试使用AJAX调用和POST方法将通过html2canvas
完成的屏幕截图发送到服务器,但它没有发生。当我尝试通过在相关行设置断点来使用Firebug调试脚本时,我的应用程序没有到达断点。
这是我的客户端代码(使用jQuery):
$("#excel").on("click", function (e) {
e.preventDefault();
html2canvas($("#placeholder").get(0), {
onrendered: function (canvas) {
//document.body.appendChild(canvas);
var img = canvas.toDataURL().replace(/^data[:]image\/(png|jpg|jpeg)[;]base64,/i, "");
$.ajax({
"type": "POST",
"url": "Default.aspx/MyMethod",
"data": {
"imageData": img //Send to WebMethod
}
}).done(function (o) {
console.log(["Response:", o]);
});
}
});
这是我的服务器端ASPX WebMethod:
[WebMethod()]
public static void MyMethod(string imageData)
{
string fileNameWitPath = "D:/Kabir/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();
}
}
当我尝试在Firebug中看到它时,它的状态为200 OK
。为什么会这样?
答案 0 :(得分:0)
你有无效的JSON:
"type": "POST", "url": "Default.aspx/MyMethod", "data": { "imageData": img //Send to WebMethod }
问题在于您的画布数据,请使用此选项。并只需替换var img =“image”;变量作为您的愿望输入
$(document).ready(function () {
$("#excel").on("click", function (e) {
e.preventDefault();
//document.body.appendChild(canvas);
var img = "image";
$.ajax({
type: "POST",
url: "Default.aspx/MyMethod",
data: JSON.stringify({ imageData: img }),
contentType: "application/json; charset=utf-8"
}).done(function (o) {
console.log(["Response:", o]);
});
});
});
答案 1 :(得分:0)
在您的客户端
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#excel").on("click", function (e) {
e.preventDefault();
//document.body.appendChild(canvas);
var img = "image";
$.ajax({
type: "POST",
url: "Default.aspx/MyMethod",
data: JSON.stringify({ imageData: img }),
contentType: "application/json; charset=utf-8"
}).done(function (o) {
console.log(["Response:", o]);
});
});
});
</script>
<input type="button" id="excel" title="something" />
关于代码
[WebMethod()]
public static void MyMethod(string imageData)
{
string fileNameWitPath = "D:/Kabir/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();
}
}
}