【问题标题】:json Webservice that will pass Base64 code that will convert to imagejson Webservice,它将传递将转换为图像的 Base64 代码
【发布时间】:2013-12-17 23:14:26
【问题描述】:

规格:

  • 服务器 2012
  • 框架 3.5

我已经在服务器http://ipaddress/eSignatureWS.asmx?op=SaveImg.部署了我的webservice

问题:当我将 ajax 数据类型更改为“json”或“text”时,它会出现内部服务器错误,但当我使用“script”时它不会出现错误,但它不会'不保存图像。当我在本地尝试它时,它使用“json”/“text”工作,但是当我部署它时它就不再工作了。

这是我的代码:

jQuery

function signatureSave() {

var canvas = document.getElementById("newSignature"); // save canvas image as data url (png format by default)
var data = canvas.toDataURL("image/png");
var signatureData = data.replace(/^data:image\/(png|jpg);base64,/, "");


var donum = "sampleImg"; //imagename
$.support.cors = true;
//var imgData = JSON.stringify(signatureData);

$.ajax({
    type: "Post",
    async: false,
    url: "http://ipaddress/eSignatureWS.asmx?op=SaveImg",
    data: { "filename": donum, "imgBit": signatureData }, 
    dataType: "json",
    success: function (data) { //alert(data);
        if (data.match("true")) {
            document.getElementById('errorImage').innerHTML = "Successfully Submitted!";
        }
        else {
            document.getElementById('errorImage').innerHTML = "Error occured.";
        }
    },
    error: function (XMLHttpRequest, testStatus, errorThrown) {
        document.getElementById('errorImage').innerHTML = errorThrown;
        alert(XMLHttpRequest + testStatus + errorThrown);
    }
})

网络服务

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string SaveImg(string filename, string imgBit)
    {
        try
        {

            string filepath = WebConfigurationManager.AppSettings["ImgLocation"].ToString();
            string imgPath = filepath + filename + ".png";

            //creates a png image signature
            using (FileStream fs = new FileStream(imgPath, FileMode.Create))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    byte[] data = Convert.FromBase64String(imgBit);
                    bw.Write(data);
                    bw.Close();
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    string strJSON = js.Serialize("true");
                    return strJSON;
                }
            }

        }
        catch (Exception ex)
        {
          
        }
    }

【问题讨论】:

    标签: json web-services jquery base64


    【解决方案1】:

    检查编辑部分!

    我猜你不知道在 ASP.NET 中默认设置为 100KB 的“maxJsonLength”属性。直到我不得不对同一主题进行最新搜索时,我才意识到这一点:)

    在 Web.Config 文件中,您可以根据需要更改此默认限制。

    <configuration>
      <system.web.extensions>
        <scripting>
          <webServices>
            <jsonSerialization maxJsonLength="1000000"/>
          </webServices>
        </scripting>
      </system.web.extensions>
    </configuration>
    

    那个是 1.000.000 字节,即 ~1MB。如果您需要通过您的网络服务发送更大的文件,您可以设置更高的值。

    检查一下,如果有效,请告诉我。

    编辑:我最近发现这不是您应该进行的唯一设置。您还应该在 web.config 文件中设置 httpruntime 节点的“maxRequestLength”属性,例如:

    <system.web>
        <httpRuntime targetFramework="4.5" maxRequestLength="10000" executionTimeout="10800" />
    </system.web>
    

    这里,maxRequestLength 以 KB 为单位,因此 10000 表示 ~10MB(准确地说是 9.77MB)。如果用户的带宽较低并且您的请求长度要求大于 10MB,那么您可以将 executionTimeout 属性设置为更高的值。以秒为单位,所以 10800 表示 3 小时。

    我想你在这里得到了图片。

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2011-04-27
      • 2023-03-11
      • 2014-04-06
      • 2017-09-12
      相关资源
      最近更新 更多