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

前端 未结 7 1768
南方客
南方客 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 = '';
        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.

提交回复
热议问题