0

I have an @Html.Action link that currently works for returning the user a pdf document from the controller. I want to be able to use an ajax call to perform the same function but I'm stuck on how or even if this can be done. I have tried several different iterations but I never get the pdf to download from the browser window. Here is what I have so far:

$('#Button1').click(function () {
    var poNum = "51970";
    $.ajax({
        type: "GET",
        data: "id= " + poNum,
        url: '@Url.Action("PoReport", new { controller = "LogisticsTools"})'

        });
  });

I can copy the Request URL from the Headers window in Chrome Dev Tools and paste it into a new page and the pdf is downloaded. I can see the pdf code in the Response window also but it just doesn't get downloaded when the button is clicked. What am I missing?

4

2 回答 2

2

You don't want to ajaxify this. Put the URL in an anchor tag and let the browser do the rest. If the browser doesn't recognise the document type or it is configured to force file downloads, the file will download as you expect (i.e. the user will see the download dialog). If the document is recognised, can be opened in the browser, and the browser is configured to open files, the file will open in the browser.

<a href='@Url.Action("PoReport", new { controller = "LogisticsTools"})'
   title="Click to Download/Open">
    Download
</a>
于 2013-09-06T16:36:35.933 回答
0

You can't download PDF file while using the ajax request to server. So instead of that you should use html actionlink. for example

@Html.ActionLink("Convert Data into PDF", "PDFView", null, new { @class= "btn btn-info" });

Above code will generate a link button and you can access Action Method of controller. Also you can use any technique to return PDF file from controller for example you can return PDF file using FileResult class.

于 2016-01-07T06:22:16.137 回答