1

I'm working on a ASP.NET MVC app where we have a requirement to produce PDF reports. The generation of these PDF reports on the back-end can take a long time (over 30 seconds). Our company uses a proxy called squid which will timeout any HTTP requests taking more than 30 seconds.

Because of the above, requesting a pdf report directly via a link or a get request will timeout.

How do I get around this? Is there a way to deliver files asynchronously? Or do I have to use a more elaborate protocol where the client requests the files and then polls the server at regular intervals to see if the file has been produced, before requesting it again for download when ready?

Valere Speranza
  • 111
  • 1
  • 6

2 Answers2

1

You could use something like signalr to poll the server for job completion and then redirect the browser once it's complete.

Just an idea but you could use hangfire, to schedule the pdf creation.

RubbleFord
  • 7,456
  • 9
  • 50
  • 80
0

I found some useful help in an existing question Handle file download from ajax post

Here's what I did. I created an ajaxDownload css class to identify links that I want to be handled asynchronously.

I created a method on the server called DownloadFile that will download the file from a specific location.

I replaced the original url that was returning the file with a url that generates the PDF and then return its file name. I pass on the filename in the success event and then use window.location.href to download the file.

    $("a.ajaxDownload").on("click", "", function(e) {
        e.preventDefault();
            var url = $(this).attr('href');
        $.ajax({
            type: "POST",
            url: url,
            error: function(res) {
                alert("an error has occured");
            },
            success: function(res) {
                var url2 = '/QuoteSummary/DownloadFile/?filename=' + res;
                window.location.href = url2;
            }});
    });
Community
  • 1
  • 1
Valere Speranza
  • 111
  • 1
  • 6