开发者

Opening PDF file from asp.net mvc/script

开发者 https://www.devze.com 2023-02-08 17:56 出处:网络
I am trying to open a pdf file by clicking a button/jquery? Does not work.The ActionLink works though?

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:

       publ开发者_高级运维ic ActionResult GetPDF()
    {
            string filename = "mypdff.pdf";
            return File(filename, "application/pdf", Server.MapPath("/Content/") + filename);
    }


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

0

精彩评论

暂无评论...
验证码 换一张
取 消