21

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 my action's controller code so far:

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

Here is my view:

@{
   doc = "Mode_d'emploi.pdf";
} 

<p>@Html.ActionLink(UserResource.DocumentationLink, "GetPdf", "General", new { fileName = doc }, null)</p>

When I mouse hover the link here is the link:

enter image description here

The problem with my code is that the pdf file is not viewed in the browser but I get a message asking me if I wand to open or save the file.

enter image description here

I know it is possible and my browser support it because I already test it with another website allowing me to view pdf directly in my browser.

For example, here is the link when I mouse hover a link (on another website):

enter image description here

As you can see there is a difference in the generated link. I don't know if this is useful.

Any idea how can I view my pdf directly in the browser?

Ralf
  • 16,086
  • 4
  • 44
  • 68
Bronzato
  • 9,438
  • 29
  • 120
  • 212

7 Answers7

51

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");
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
26

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;
}
Forty-Two
  • 7,535
  • 2
  • 37
  • 54
  • @ataravati This is correct when returning stream result. if you are returning file your answer will work – Manjay_TBAG Jun 14 '19 at 17:57
  • @TBAG, In my answer I have explained why this is not the right answer. Because it doesn't tell you why you're getting a message asking you to open or save. – ataravati Jul 02 '19 at 14:22
11

Change your code to this :

       Response.AppendHeader("Content-Disposition","inline;filename=xxxx.pdf");
       return File(filePath, "application/pdf");
Amir
  • 352
  • 3
  • 10
1

If you read the file stored in database image column, you can use like this:

public ActionResult DownloadFile(int id)
{
    using (var db = new DbContext())
    {
        var data =
            db.Documents.FirstOrDefault(m => m.ID == id);
        if (data == null) return HttpNotFound();
        Response.AppendHeader("content-disposition", "inline; filename=filename.pdf");
        return new FileStreamResult(new MemoryStream(data.Fisier.ToArray()), "application/pdf");
    }
}
TotPeRo
  • 6,561
  • 4
  • 47
  • 60
0

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.

dush88c
  • 1,918
  • 1
  • 27
  • 34
0

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

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);
            }
        }
sathish v
  • 11
  • 3