我正在尝试将html2canvas图像保存到asp.net webmethod中的服务器端代码。为此我试图通过jquery ajax click方法发送参数。在客户端代码上的每一件事都很好,因为我没有得到任何类型错误或警告,但同时我也没有得到服务器端的图像。我试图找出问题从长期但没有得到的方式或原因是它发生。这是我的客户端代码.. < / p>
$("#excel").on("click", function (e) {
e.preventDefault();
html2canvas($("#placeholder").get(0), {
onrendered: function (canvas) {
var img = canvas.toDataURL().replace(/^data[:]image\/(png|jpg|jpeg)[;]base64,/i, "");
$.ajax({
type: "POST",
url: "Default.aspx/MyMethod",
data: "img=" + img,
success: function (msg) {
alert("Data Saved: " + msg);
}
});
}
});
});
请伙计们帮助我。我完全被这种情况所震惊。 需要生命救世主。 提前谢谢。
这是我的服务器端代码..
[WebMethod]
public static void MyMethod(string img)
{
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(img);//convert from base64
bw.Write(data);
bw.Close();
}
}
}
这是我的网络服务代码..
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
[WebMethod]
public static void MyMethod(string img)
{
string path = HttpContext.Current.Server.MapPath("/") + "Test.jpg";
//string fileNameWitPath = "D:\\Kabir\\custom_name.png";
using (FileStream fs = new FileStream(path, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(img);//convert from base64
bw.Write(data);
bw.Close();
}
}
}
}
这在firebug中给出了这个错误..
System.InvalidOperationException: MyMethod Web Service method name is not valid.
at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
为什么会出现这个错误?
答案 0 :(得分:0)
我无法通过调用ASPX webmethod来创建图像。将代码移动到ASMX Web服务后,它就创建了映像。确保您对该文件夹具有权限,对于Windows,/
变量上的正斜线(fileNameWitPath
)需要为反斜杠(\
)
<强> ASMX 强>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.IO;
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// <System.Web.Script.Services.ScriptService()> _
[System.Web.Services.WebService(Namespace = "http://tempuri.org/")]
[System.Web.Services.WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod()]
public void MyMethod(string img)
{
string path = HttpContext.Current.Server.MapPath("/") + "Test.jpg";
using (FileStream fs = new FileStream(path, FileMode.Create)) {
using (BinaryWriter bw = new BinaryWriter(fs)) {
byte[] data = Convert.FromBase64String(img);
bw.Write(data);
bw.Close();
}
}
}
}
<强>脚本强>
$("#excel").on("click", function (e) {
e.preventDefault();
html2canvas($("#placeholder").get(0), {
onrendered: function (canvas) {
var img = canvas.toDataURL().replace(/^data[:]image\/(png|jpg|jpeg)[;]base64,/i, "");
$.ajax({
type: "POST",
url: "WebService1.asmx/MyMethod",
data: "img=" + img,
success: function (msg) {
alert("Data Saved: " + msg);
}
});
}
});