如何使用jQuery将图像发布到WebMethod?

时间:2014-11-02 19:04:11

标签: jquery asp.net ajax canvas webmethod

我有一个像后面的ASP.NET WebMethod。

    [WebMethod]
    public static void AddCameraImage(string imageArray)
    {
       //do something
    }

我正在从canvas读取图像,并希望使用以下代码将图像发布到上述方法:

$("#imageUl li").each(function (index, item) {
  console.log($(this).find("canvas")[0].toDataURL("image/png"));
  //This log is working properly. It's logging a very big string.  "data:image/png;base64,iVBORVl3APD/AwWGMo8iBuQmCC...."
  //I've truncated the string here.
  $.ajax({
    type: "POST",
    url: "Default.aspx/AddCameraImage",
    data: '{ "imageArray": "' + $(this).find("canvas")[0].toDataURL("image/png") + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
       },
    failure: function (response) {
          alert("Failed to add image");
       }
    });
 });

但它显示POST http://localhost:1315/Default.aspx/AddCameraImage 500 (Internal Server Error)

但是如果我将ajax请求的data属性更改为data: '{ "imageArray": "' + "test_hardcode_data" + '"}',,它就能正常工作。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我认为你应该在web.config中增加maxJsonLength。如果我记得很清楚,默认值大约是100k,可能还不够你的照片。 并且还更改“failure:function(response){”in“error:function(a,b,c){”,在里面设置一个断点并检查3个变量的内容,它应该告诉你大小限制错误。