Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I stored images in database in base_64 format. Fetching that data from a controller to html using Ajax. While fetching record from in controller, I get the following error in custom filter exception:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

cshtml - Ajax callled:

function imagepop(no) {
     $.ajax({
                type: "POST",
                contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                url:"/Controller/GetImage",
                //data: arr,
                data: { 'No': no, 'filetype': obj, 'pageNo': pageNo },
                //dataType: "json",
                success: function (result) {
                    debugger;
                    $("#loader_Prcessing").hide();
                    if (result.Status == "200") {   
                    }
                }
            });
}        


Controller:

public JsonResult GetImage(string no, string filetype, string pageNo)
{
    var jsonResult = Json(OutputInfo.lstUploadImgVideoDoc, JsonRequestBehavior.AllowGet);

    jsonResult.MaxJsonLength = int.MaxValue;

    return Json(jsonResult);
}


web.config:

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



I stored images in database in base_64 format. Fetching that data from a controller to html using Ajax. While fetching record from in controller, I get the following error in custom filter exception:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

cshtml - Ajax callled:

function imagepop(no) {
$.ajax({
type: "POST",
contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
url:"/Controller/GetImage",
//data: arr,
data: { 'No': no, 'filetype': obj, 'pageNo': pageNo },
//dataType: "json",
success: function (result) {
debugger;
$("#loader_Prcessing").hide();
if (result.Status == "200") {
}
}
});
}
Controller:

public JsonResult GetImage(string no, string filetype, string pageNo)
{
var jsonResult = Json(OutputInfo.lstUploadImgVideoDoc, JsonRequestBehavior.AllowGet);

jsonResult.MaxJsonLength = int.MaxValue;

return Json(jsonResult);
}
web.config:

<system.web.extensions>
<scripting>
<webservices>
<jsonserialization maxjsonlength="2147483647">




Is there any solution for this at the config level? OR any suggestion with minimum changes in code.

What I have tried:

I tried many thing as suggest on google but not succeded,Please help on this
Posted
Updated 2-Feb-23 17:40pm

1 solution

First, storing images in the database as anything other than binary is just going to make the image data quite a bit larger than if you just stored the bytes in a binary field, especially using base-64 encoding.

Next, wrapping all that in a json string only makes it worse.

The <jsonserialization maxjsonlength="" /> value in web.config is ignored now-a-days. Yes, there is a way around this, and you are already doing it!

The problem with your code is you're throwing out the good JsonResult object and creating an entirely new one and returning that. The fixed code should look more like this:
C#
public JsonResult GetImage(string no, string filetype, string pageNo)
{
    var jsonResult = Json(OutputInfo.lstUploadImgVideoDoc, JsonRequestBehavior.AllowGet);

    jsonResult.MaxJsonLength = int.MaxValue;

    return jsonResult;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900