In ASP MVC, how can I return a new view AND a file to the user?

早过忘川 提交于 2019-11-29 14:28:20

I've broken this down into two separate actions on the Home Controller, using the FinalPage action as the view you get redirected to and the GetFile action as the one to return the file itself.

Controller

    public ActionResult GetFile()
    {
        return File(@"path to pdf.pdf", "application/pdf");
    }

    public ActionResult FinalPage()
    {
        return View();
    }

View

<script>

    function showfile() {
        window.open('<%= Url.Action("GetFile", "Home")%>')
    }

</script>

<%= Html.ActionLink("click", "FinalPage", "Home", null, new { onclick = "return showfile();" }) %>

This will open up a new window and get the file returned to display, but also move the other browser window onto the final page on the same click.

Hope this helps.

Edit

Updated to run off a submit button as per comment ... in answer to the comment, yes you can do it off a submit button :-)

<script>

    function showfile() {
        window.open('<%= Url.Action("GetFile", "Home")%>')
    }

</script>

<% using(Html.BeginForm("FinalPage", "Home")) { %>

    <input type="Submit" value="click" onclick="return showfile();" />

<% } %>

Hope this helps :-)

WestDiscGolf, to download on the same page use '_self' :

function showfile() {
        window.open('PATH','_self')
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!