1

I am trying to open a pdf file by clicking a button/jquery? Does not work.The ActionLink works though?

 <h2>
    GetPDF</h2>
<%= Html.ActionLink("works", "GetPDF") %>
<button id="btn">
    does not work
</button>

<script type="text/javascript">

    $(document).ready()
    {
        $("#btn").click(
        function() {
            var url = "/Home/GetPDF";
            var data = "";
            $.ajax(
        {
            url: url,
            type: 'POST',
            data: data,
            error: function(err) {
                alert('error');
            }
        }
);
        }

    )
    };

</script>

the controller looks like this:

       public ActionResult GetPDF()
    {
            string filename = "mypdff.pdf";
            return File(filename, "application/pdf", Server.MapPath("/Content/") + filename);
    }

1 Answer 1

2

Its because you are making an ajax call. The resulting file is in the ajax object. Perhaps you would like to use window.location(...)?

Sign up to request clarification or add additional context in comments.

1 Comment

I completely agree with your assessment, just wanted to add that one could style the link as a button (css inline-block w/ borders, backgrounds, etc.) and possibly achieve the desired effect without JavaScript.

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.