0

I am attempting to download a PDF file when a certain page loads in my MVC app. I have the following JS code that fires when the document is ready:

function downloadTripReport() {
    $.ajax({
        url: '/CompanyHomepage/Download?tripReportId=21',
        success: function (data) {
            var blob = new Blob([data]);
            var link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = "TripReport.pdf";
            link.click();
        }
    });
}

I actually got this solution from this SO post.

The following MVC method is being called:

public FileResult DownloadTripReport(int tripReportId)
{
    try
    {
        var userId = this.TripReportData.GetItem(tripReportId).UserId;

        var pdf = this.AzureStorage.RetrieveBlob(this.GetAzureContainerName(userId),
            this.GetAzureTripReportBlobName(tripReportId));

        return File(pdf, "application/pdf", "TripReport.Pdf");
    }
    catch (Exception exception)
    {
       ...
    }
}

Essentially this method retrieves a PDF from Azure storage and sends it back to the calling client. This code works correctly. I know this because it's being called in other ways and it correctly sends a PDF to the client that can then be viewed.

The problem is this. When the AJAX call is made, a PDF file is downloaded to the client. But when the PDF is clicked on, the OS says the format is incorrect (i.e, it's not a PDF). I've checked the size of the downloaded file and it is correct. But, for some reason, the OS does not think it's a valid PDF. So, I'm guessing the issue has to be with the AJAX call.

Community
  • 1
  • 1
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • What happens if you call the mvc action directly? Does that work? – scheien Mar 18 '16 at 13:43
  • @scheien - Yes it does. The MVC method works correctly and is not the issue. – Randy Minder Mar 18 '16 at 13:46
  • Try this : var cd = new System.Net.Mime.ContentDisposition { FileName = "Activity Report" + ".pdf", // always prompt the user for downloading, set to true if you want // the browser to try to show the file inline Inline = true }; Response.AppendHeader("Content-Disposition", cd.ToString()); return File(pdf, "application/pdf"); – Varun Vasishtha Mar 18 '16 at 13:47
  • And I have one more method in which you can download pdf through ajax but dont have to handle blobs, basically it does same, it also goes on window.location, if you are interested then please let me know – Varun Vasishtha Mar 18 '16 at 13:49
  • @VarunVasishtha - As I said, the issue is not the MVC code. When called directly, the PDF file is downloaded correctly and is able to be displayed. – Randy Minder Mar 18 '16 at 13:56
  • 1
    You are not able to download a file using AJAX calls (http://stackoverflow.com/questions/8771342/file-download-script-doesnt-work-when-called-from-ajax). Instead, try to redirect the user `/CompanyHomepage/Download?tripReportId=21` – Andy T Mar 18 '16 at 14:53

0 Answers0