ASP.NET MVC: Displaying a PDF Document in the Browser

Returning a pdf document (or any file for that matter) is very simple using ASP.NET MVC due to the fact that action methods can return a result of type FileResult or FilePathResult. To return a file stored in the App_Data directory, simply do the following:

public FileResult GetFile(string fileName)
    string path = AppDomain.CurrentDomain.BaseDirectory + "App_Data/";            
    return File(path + fileName, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);

But, despite the fact that we have indicated that the MIME type is ‘application/pdf’, the user will be prompted with a download dialog box instead of the pdf simply being displayed in the browser. This is a shortcoming in the ASP.NET MVC framework that can be fixed by adding a line to the header of the response. To make the browser display the pdf file, add the following line of code to your action method:

public FileResult GetFile(string fileName)
    // Force the pdf document to be displayed in the browser
    Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName + ";");

    string path = AppDomain.CurrentDomain.BaseDirectory + "App_Data/";            
    return File(path + fileName, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);

Adding the value ‘inilne’ to the Content-Disposition attribute indicates to the browser that it should be displayed immediately rather than downloaded by the user.


6 Responses to “ASP.NET MVC: Displaying a PDF Document in the Browser”

  1. John Melville Says:

    Many thanks.

    The exact answer to the question I was looking for and an answer I would have not soon stumbled upon.

    John Melville

    • Soumitra Says:

      I have a large number (100000+) of pdf files, which are available to the users of the website to view, based upon proper authentication and authorozation. What is the best practice around the location to store these pdfs (i am not storing them in a database). So far the options are below:

      1. In the app_data
      2. Another folder outside of the webroot of the app.

      I will protect these files using a HTTPHandler from unauthorized acess and downloads.

      Any ponters will be highly appreciated.

      Regards, Soumitra

      • Nick Olsen Says:

        I’m by no means an expert on this but I don’t think there is a really a difference. All in all, app_data is just another folder in your application. I think app_data is protected from direct browsing by IIS so you do get that extra layer of “security” but if you are using an HTTPHanlder to prevent unauthorized access then it shouldn’t matter either way. But, again, I do not claim to be an expert in security at all.

  2. Jim Says:

    I get a Corrupted Content Error when I adapted my MVC app based on your example. My pdf file is stored in the database and not in the App_Data folder. I’m providing the fileContent at byte[].

  3. chrjs Says:

    Just for the records. If you add the additional header and provide the filename in the File() call, there will be the error as Jim said.
    There is no way to make all browsers happy with this kind of solution.
    Url rewriting is the correct approach to make all browsers happy.

  4. Nitesh Kumar Says:

    Well i am just a beginner in
    In my project i have a created a elibrary ,so i have more than 20. .pdf files,they are first saved in sql database and then to a folder named bookpdf in the solution explorer.Now my question is that i display the books via datalist hence i need certain query that if i click on book (a) then only book a will be show not book (b) do you have any solution to my query???if yes then thanks and remember that i am a beginner in,its hard for me to understand complex codes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: