0

I'm struggling with maxJsonLength error. I'm passing very long xml string (~4MB) from controller action to javascript method in a View. This method proceses data and send it back with AJAX to another controller method, and here I'm getting error.

I've set MaxJsonLength in JsonResult object, in the controller method, and the is no problem with passing this string from controller to view. But when I'm try to pass it back after processing I'm getting error.

This is my Controller method whitch prepare data:

private JsonResult PrepareXml(int someId)
{
     // preparing data...
     string xmlData = "..."; //(~4Mb)
     JsonResult res = Json(xmlData);
     res.MaxJsonLength = 10000000; // 10Mb
     return res;
}

And this is my Controller method whitch is trying to proces data passed from ajax method:

private JsonResult GetProcesedXml(string resultXml)
{
     // This method is not ivoking
}

And below is my script method:

var data = {
        someId: rowId,
    };
$.ajax({
        type: "POST",
        url: "MyController/PrepareXml",
        data: data,
        contentType: "application/json; charset=utf-8",
        success: function (result) {
                // proces the result (big xml file)
                //...
                var processedResult = ""; // size of processedResult is a little bit bigger than 'result'
                var data2 = {
                    resultXml: processedResult
                };

                $.ajax({
                    type: "POST",
                    url: "MyController/GetProcesedXml",
                    data: data2,
                    contentType: "application/json; charset=utf-8",
                    success: function (r) {
                        alert('success');
                    },
                    error: function (data) {
                        alert(data.responseText);
                    }
                });
            }        
 });

What I've tried already:

<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" /> //(max value)
<requestLimits maxAllowedContentLength="2097151000" /> //(max value)
<httpRuntime maxRequestLength="10000" /> //(~10MB)
daniell89
  • 1,832
  • 16
  • 28

1 Answers1

0

I've found some workaround for this problem. I've changed content type in Ajax method to "text/plain" and used JSON.stringify on my xml file data.

var data2 = { resultXml: processedResult };

$.ajax({
    type: "POST",
    url: "MyController/GetProcesedXml",
    data: JSON.stringify(data2),
    contentType: "text/plain",
    success: function (r) {
                alert('success');
    },
});

Another change is in controller. I've read file from input stream and parse it to form JSON type:

private JsonResult GetProcesedXml()
{
     JObject json = null;
     Stream request = Request.InputStream;
     using (StreamReader sr = new StreamReader(stream))
     {
           stream.Seek(0, System.IO.SeekOrigin.Begin);
           json = JObject.Parse(sr.ReadToEnd());
     }
     string xmlSigninigResult = json["resultXml"].ToString();
     // rest of the method
}
daniell89
  • 1,832
  • 16
  • 28