首页 > 解决方案 > 错误“字符串的长度超过了 maxJsonLength 属性上设置的值。”

问题描述

我正在将图像数据从后端传递到前端。如果图像尺寸小,一切正常。否则我会收到以下错误:'使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了 maxJsonLength 属性设置的值。我研究了一些建议,但到目前为止还没有运气。

    public static ModelResponse GetData(int imageID, string docType)
    {
        var imageData = DataConnection.Test.GetData(fileID).FirstOrDefault();

        ImageFileDTO imageFile = new ImageFileDTO
        {
            ImageFileData = Convert.ToBase64String(imageData.FileData)
        };

        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        serializer.MaxJsonLength = Int32.MaxValue;

        var result = serializer.Serialize(imageFile);
        return new ModelResponse(result);
    }

我还在网络配置中设置了 maxJsonLenght

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

标签: javascriptc#

解决方案


<jsonSerialization maxJsonLength="2147483644" />

尝试这个。


推荐阅读