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

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;
}

Change your code to this :

       Response.AppendHeader("Content-Disposition","inline;filename=xxxx.pdf");
       return File(filePath, "application/pdf");

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");
}

Tags:

C#

Asp.Net Mvc