I am returning json result from controller action and result includes the binary image and i want to show it in view using javascript. but it shows the Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

Action which called using ajax on page load

public ActionResult LoadBooks()
            var v = from t in db.BookTbls
                    where t.IsDisplay==true
                    select new BookVM

            List<BookVM> list = new List<BookVM>();
            foreach(var temp in v.ToList())
                BookVM obj = new BookVM();
                obj.BookID = temp.BookID;
                obj.BookName = temp.BookName;
                obj.BookPrize = temp.BookPrize;
                obj.BookImagepath = temp.BookImagepath;
                var base64 = Convert.ToBase64String(temp.BinaryImage);
                var imgsrc = string.Format("data:image/jpg;base64,{0}", base64);
                obj.BImage = imgsrc;//BImage is string variable from model
            return Json(v.OrderBy(p => p.BookName).ToList(), JsonRequestBehavior.AllowGet);

in short i just want that result in view, its not even goes in success(result) just showing console error mentioned above.

1 Answers

pool pro On

Welcome to Stackoverflow,

The MaxJsonLength property cannot be unlimited, is an integer property that defaults to 102400 (100k).

You can set the MaxJsonLength property on your web.config: the max you can have i believe is 2147483644.

               <jsonSerialization maxJsonLength="50000000"/>

As a workaround, you can do the following within your Controller (or anywhere really):

var serializer = new JavaScriptSerializer();

// For simplicity just use Int32's max value.
// You could always read the value from the config section mentioned above.
serializer.MaxJsonLength = Int32.MaxValue;

var resultData = new { Value = "foo", Text = "var" };
var result = new ContentResult{
    Content = serializer.Serialize(resultData),
    ContentType = "application/json"
return result;

Mirosoft Documentation.