How can I open a pdf file directly in my browser?

后端 未结 7 546
陌清茗
陌清茗 2020-12-25 15:14

I would like to view a PDF file directly in my browser. I know this question is already asked but I haven\'t found a solution that works for me.

Here is

相关标签:
7条回答
  • 2020-12-25 15:30

    Although previous posts are often correct; I think most of them are not best practice! I'd like to suggest to change action return types to FileContentResult and usereturn new FileContentResult(fileContent, "application/pdf"); at the end of action body.

    0 讨论(0)
  • 2020-12-25 15:31

    Change your code to this :

           Response.AppendHeader("Content-Disposition","inline;filename=xxxx.pdf");
           return File(filePath, "application/pdf");
    
    0 讨论(0)
  • 2020-12-25 15:32

    Yes You Can do It Simply by redirecting . it ends extension like u need , .pdf ..

    protected void OpenPdfPdf_Click(object sender, EventArgs e)
            {
                Response.Redirect("jun.pdf");
            }
    

    Or another Method ,its opens like .aspx page--

     protected void OpenPdf_Click(object sender, EventArgs e)
            {
                string path = Server.MapPath("jun.pdf");
                //or you want to load from url change path to
    //string path="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";   
                WebClient client = new WebClient();
                Byte[] buffer = client.DownloadData(path);
                if (buffer != null)
                {
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-length", buffer.Length.ToString());
                    Response.BinaryWrite(buffer);
                }
            }
    
    0 讨论(0)
  • 2020-12-25 15:36

    The reason you're getting a message asking you to open or save the file is that you're specifying a filename. If you don't specify the filename the PDF file will be opened in your browser.

    So, all you need to do is to change your action to this:

    public ActionResult GetPdf(string fileName)
    {
        string filePath = "~/Content/files/" + fileName;
        return File(filePath, "application/pdf");
    }
    

    Or, if you need to specify a filename you'll have to do it this way:

    public ActionResult GetPdf(string fileName)
    {
        string filePath = "~/Content/files/" + fileName;
        Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);        
    
        return File(filePath, "application/pdf");
    }
    
    0 讨论(0)
  • 2020-12-25 15:40

    If you are using Rotativa package to generate PDF, Then don't put a name to file with FileName attribute like below example.

     return new PartialViewAsPdf("_JcPdfGenerator", pdfModel);
    

    Hope this is helpful to someone.

    0 讨论(0)
  • 2020-12-25 15:47

    Instead of returning a File, try returning a FileStreamResult

    public ActionResult GetPdf(string fileName)
    {
        var fileStream = new FileStream("~/Content/files/" + fileName, 
                                         FileMode.Open,
                                         FileAccess.Read
                                       );
        var fsResult = new FileStreamResult(fileStream, "application/pdf");
        return fsResult;
    }
    
    0 讨论(0)
提交回复
热议问题