0

I have the following code, being called by an ajax, which is converting a pdf to a base64 string and then sending the result as a json:

public object LoadPDF(string fileName)
{
    string filePath = Server.MapPath(new Uri(fileName).LocalPath);
    PdfLoadedDocument loadedDoc = new PdfLoadedDocument(filePath);
    MemoryStream stream = new MemoryStream();
    loadedDoc.Save(stream);

    byte[] docBytes = stream.ToArray();
    loadedDoc.Close(true);

    return Json(new { data = "data:application/pdf;base64," + Convert.ToBase64String(docBytes.ToArray()) });
}

This is working fine except that when the file is above 2MB. I get the following error message in my ajax success:

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

Any work around for this please?

refresh
  • 1,319
  • 2
  • 20
  • 71
  • @TetsuyaYamamoto: I tried the solution of the other question:var jsonResult = Json(Convert.ToBase64String(docBytes.ToArray()), JsonRequestBehavior.AllowGet); jsonResult.MaxJsonLength = int.MaxValue; return Json(new { data = "data:application/pdf;base64," + jsonResult }); However this is not sending the data of the base64. Any help here? – refresh Feb 07 '19 at 08:31
  • Why you want to set the PDF data as Base64 string, are you want to pass it into a PDF viewer or download it from browser? If you want to do the second, simply pass the filename and download it using `ActionResult` that using GET method which has `return File(docBytes, "application/pdf", "example.pdf")`. – Tetsuya Yamamoto Feb 07 '19 at 08:36
  • No, I need it as viewer. – refresh Feb 07 '19 at 08:36

0 Answers0