How to open PDF file in a new tab or window instead of downloading it (using asp.net)?

前端 未结 7 1709
南方客
南方客 2020-11-28 10:16

This is the code for downloading the file.

System.IO.FileStream fs = new System.IO.FileStream(Path+\"\\\\\"+fileName, System.IO.FileMode.Open, System.IO.File         


        
相关标签:
7条回答
  • 2020-11-28 10:31

    you can return a FileResult from your MVC action.

    *********************MVC action************

        public FileResult OpenPDF(parameters)
        {
           //code to fetch your pdf byte array
           return File(pdfBytes, "application/pdf");
        }
    

    **************js**************

    Use formpost to post your data to action

        var inputTag = '<input name="paramName" type="text" value="' + payloadString + '">';
        var form = document.createElement("form");
        jQuery(form).attr("id", "pdf-form").attr("name", "pdf-form").attr("class", "pdf-form").attr("target", "_blank");
        jQuery(form).attr("action", "/Controller/OpenPDF").attr("method", "post").attr("enctype", "multipart/form-data");
        jQuery(form).append(inputTag);
        document.body.appendChild(form);
        form.submit();
        document.body.removeChild(form);
        return false;
    

    You need to create a form to post your data, append it your dom, post your data and remove the form your document body.

    However, form post wouldn't post data to new tab only on EDGE browser. But a get request works as it's just opening new tab with a url containing query string for your action parameters.

    0 讨论(0)
  • 2020-11-28 10:36

    Instead of loading a stream into a byte array and writing it to the response stream, you should have a look at HttpResponse.TransmitFile

    Response.ContentType = "Application/pdf";
    Response.TransmitFile(pathtofile);
    

    If you want the PDF to open in a new window you would have to open the downloading page in a new window, for example like this:

    <a href="viewpdf.aspx" target="_blank">View PDF</a>
    
    0 讨论(0)
  • 2020-11-28 10:40

    this may help

    Response.Write("<script>");
    Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');");
    Response.Write("</script>");
    
    0 讨论(0)
  • 2020-11-28 10:40

    Use this code. This works like a champ.

    Process process = new Process();
    process.StartInfo.UseShellExecute = true;
    process.StartInfo.FileName = outputPdfFile;
    process.Start();
    
    0 讨论(0)
  • 2020-11-28 10:43

    You have to create either another page or generic handler with the code to generate your pdf. Then that event gets triggered and the person is redirected to that page.

    0 讨论(0)
  • 2020-11-28 10:48
    Response.ContentType = contentType;
    HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
    Response.BinaryWrite(fileContent);
    

    And

    <asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../>
    

    In javaScript:

    <script type="text/javascript">
        function openInNewTab() {
            window.document.forms[0].target = '_blank'; 
            setTimeout(function () { window.document.forms[0].target = ''; }, 0);
        }
    </script>
    

    Take care to reset target, otherwise all other calls like Response.Redirect will open in a new tab, which might be not what you want.

    0 讨论(0)
提交回复
热议问题