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

后端 未结 7 548
陌清茗
陌清茗 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: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");
    }
    

提交回复
热议问题