我从控制器操作返回json结果,结果包括二进制图像,我想使用javascript在视图中显示它。但它显示Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
在页面加载时使用Ajax调用的操作
public ActionResult LoadBooks()
{
var v = from t in db.BookTbls
where t.IsDisplay==true
select new BookVM
{
BookID=t.BookID,
BookName=t.BookName,
BookPrize=t.BookPrize,
BookImagepath=t.BookImagepath,
BinaryImage=t.BinaryImage
};
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
list.Add(obj);
}
return Json(v.OrderBy(p => p.BookName).ToList(), JsonRequestBehavior.AllowGet);
}
简而言之,我只是希望看到结果,它甚至没有成功(结果)
只是显示上面提到的控制台错误。
答案 0 :(得分:-1)
欢迎使用Stackoverflow
MaxJsonLength属性不能是无限的,它是一个整数属性,默认为102400(100k)。
您可以在web.config上设置MaxJsonLength属性:我认为您可以拥有的最大值是2147483644。
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
作为解决方法,您可以在Controller内(或在任何实际位置)执行以下操作:
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;