0

I have a web method in which I convert a HTML to PDF and then save it to a local folder, I want the user to download that file without making a post back, so I'm trying to make an AJAX POST call into a web method to get the byte array and then convert it into a PDF, the problem is that I get an error 500:

{Message: "There was an error processing the request.", StackTrace: "", ExceptionType: ""}

Although I know the web method triggers, because when placing a breakpoints it stops there and I can actually see the binary array before the return as well as the created file on the folder, I still get the error massage, here is my code:

C#:

[WebMethod]
public static byte[] getfile(string one, string two)
{
    HttpContext context = HttpContext.Current;
    HtmlToPdf converter = new HtmlToPdf();

    converter.Options.MinPageLoadTime = 10;
    converter.Options.MaxPageLoadTime = 30;

    PdfDocument doc = converter.ConvertUrl("http://localhost/dashboard_pdf.aspx?one=" + one+ "&" + "two=" + two);

    string appPath = HttpContext.Current.Request.ApplicationPath;
    Random rnd = new Random();
    int num = rnd.Next(1, 1000000);
    string path = context.Server.MapPath(appPath + "/Web/" + num + ".pdf");

    doc.Save(path);   
    doc.Close();

    FileStream stream = File.OpenRead(path);
    byte[] fileBytes = new byte[stream.Length];

    stream.Read(fileBytes, 0, fileBytes.Length);
    stream.Close();

    byte[] b1 = System.IO.File.ReadAllBytes(path);

    return fileBytes;
}

JS:

$.ajax({
    type: "POST",
    url: "dashboard.aspx/getfile",
    contentType: "application/json; charset=utf-8",
    data: "{'one':\"" + one+ "\", 'two':\"" + two + "\" }",
    dataType: "json",
    processData: false,
    success: function (data) {
        data = data.d;

        var byteArray = new Uint8Array(data);
        var a = window.document.createElement('a');

        a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/pdf' }));

        a.download = "Dashboard";

        document.body.appendChild(a)
        a.click();
        document.body.removeChild(a)
    }
});

Any ideas?

Thanks.

4
  • dataType shouldn't be "json" and I don't think it supports bytes. You probably want to use a different api. Commented Oct 8, 2018 at 20:57
  • Maybe try this? Commented Oct 8, 2018 at 20:59
  • I know for sure that this method should work, because this is practically a copy paste from a method that I have converting a word file into a byte array, the difference is that in this case the file is located in a folder, my other method has the file in the DB, that's why I don't know why this in particular doesn't work. Commented Oct 8, 2018 at 21:03
  • Interesting, seems like it should not work at. Commented Oct 8, 2018 at 22:10

2 Answers 2

1

I solve this by adding this into my web.config:

 <system.web.extensions>
    <scripting>
      <webServices>
        <!-- Update this value to change the value to a larger value that can accommodate your JSON Strings -->
        <jsonSerialization maxJsonLength="86753090" />
      </webServices>
    </scripting>
  </system.web.extensions>
Sign up to request clarification or add additional context in comments.

Comments

0
 var jsonResult = Json(model, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;

In this way you return the maximum byte array and can be able to convert to PDF and download

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.