我正在尝试使用c#页面将画布从签名板保存为.png。我正在使用以下ajax:
var donum = "xxxxxxxxxx.png"; //imagename
var usersessionid = "xxxxxxxxxx";
// Generate the image data
var Pic = signaturePad.toDataURL();
Pic = Pic.replace('data:image/png;base64,', '');
// Sending the image data to Server
$.ajax({
type: 'POST',
url: '../ASPFiles/Save_Picture.aspx/UploadPic',
data: '{ "imageData" : "' + Pic + '", "fname" :"' + donum + '", "usersession" : "' + usersessionid + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: OnComplete,
error: function (xhr, textStatus, errorThrown) {
alert('request failed->' + textStatus);
}
});
以下c#:
[WebMethod()]
public static void UploadPic(string imageData, string fname, string usersession)
{
string maxinactiveinterval = System.Configuration.ConfigurationManager.AppSettings["MaxUserTimeout"];
string strConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
string queryString = (removed)
using (SqlConnection connection = new SqlConnection(strConnectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
if (reader.HasRows)
{
string Pic_Path = Path.Combine(HttpContext.Current.Server.MapPath("~/Uploads/Signatures/"), fname);
using (FileStream fs = new FileStream(Pic_Path, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
}
// Call Close when done reading.
reader.Close();
}
}
我在同一台服务器上的另一个网站上使用它没有任何问题,但在这个网站上我只得到一个“请求失败 - > parseerror”响应。 web.config确实包含:
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
虽然在工作现场它运行时没有输入这些协议。
答案 0 :(得分:0)
UploadPic
函数需要将有效的JSON返回给Ajax。即使返回像"SUCCESS"
这样的简单字符串也可以。
只有网络服务需要HttpGet
和HttpPost
协议(即具有[WebMethod()]
属性)。