How To Open PDF File In New Tab In MVC Using C#

In this post, we will learn about how to open PDF or other files in a new tab using C#. For this example, first we need to return a file from MVC Controller then open the file in a new tab from view. For this, I will set return type "FileResult" from MVC controller and return "File" with a byte Array of the file and its content type. Let's start coding.
Step 1

First, create a new project of MVC from File -> New -> Project.
Step 2

Select ASP.NET Web Application (.Net Framework) for creating an MVC application and set Name and Location of Project. 
Step 3

After setting the name and location of the project, open another dialog. From this dialog select MVC project and click OK. 
After creating a project create one controller method inside the home controller and name that "GetReport". and write the below code in this method.
  1. public FileResult GetReport()  
  2. {  
  3.     string ReportURL = "{Your File Path}";  
  4.     byte[] FileBytes = System.IO.File.ReadAllBytes(ReportURL);  
  5.     return File(FileBytes, "application/pdf");  

Above method returns FileBytes, and Content type is "application/pdf".
Create one function for an open PDF file in a new tab. 
  1. function GetClientReport() {  
  2.'/{ControllerName}/GetReport, "_blank");  
  3. }; 
The above function will open a new tab in the browser and call controller GetReport() method and this method returns file ,and browser is displayed in an opened tab.