Hau Le
Hau Le

Reputation: 677

How to download file PDF using FileResult but "only" via $ajax request?

This is the web method definition:

[HttpPost]
public FileResult GenerateReport(string Id) {
     //............Code .................//
 return File(response.ReportContents, "application/pdf");

} 

and here is my jQuery ajax call to web method:

$(".ValuationReport").bind('click', function () {
        $.ajax({
            url: "https://localhost.com/Report/GenerateReport
            type: "POST",
            data: { Id: "00AFCA2F-6809-4FF4-BA32-125EAEBB1321" },
            success: function (data) {

            },
            error: function () {
                alert("error");
            }
        });
    });

Please help me.

Upvotes: 0

Views: 1832

Answers (2)

JIA
JIA

Reputation: 1495

you havent mentioned the problem you are really facing with your current approach, as it happens you cannot download the file using the XMLHTTPRequest, with your current approach a download dialog should appear as a response to your ajax request because the browser will unable to understand the "application/pdf" ContentType and will try to download the file

Upvotes: 1

ZippyV
ZippyV

Reputation: 13018

You can't download files using an ajax request.

Upvotes: 1

Related Questions