1

My requirement is to open a file in a new window/tab upon clicking the download link. The file is always downloaded despite setting the content disposition to 'inline' as suggested in the below post.

How to open PDF file in a new tab or window instead of downloading it (using asp.net)?

My HTTP response is

HTTP/1.1 200 OK
Cache-Control: private, s-maxage=0
Content-Type: application/octet-stream
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 5.2
content-disposition: inline;filename=FileName.pdf
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 141954

My Controller Action is as below -

public async Task<FileContentResult> Index(string fileName)
    {
        byte[] document = await getFileContent("SomePathForTheFile");
        HttpContext.Response.AddHeader("content-disposition", "inline;filename=" + document.FileName);
        return File(document, "application/octet-stream");
    }

Suggestions to make the file open automatically in a new tab/window instead of just being downloaded would be of great help! Thanks!

Community
  • 1
  • 1
jothi
  • 332
  • 1
  • 5
  • 16

2 Answers2

3

Found the answer.

2 things have to be done for the file to open in a new window -

1.content-disposition must be set to inline

2.the content type should be set to the type corresponding to the file being downloaded -

application/pdf - for pdf files

application/xml - for xml files

application/vnd.ms-excel - for xls files and so on.

jothi
  • 332
  • 1
  • 5
  • 16
0

Try the following code.

@Html.ActionLink("Home","Index",null,new { @target="_blank" })

Since we are returning to the view mode, there is no need to mention about header response

public async Task<FileContentResult> Index(string fileName)
    {
        byte[] document = await getFileContent("SomePathForTheFile");
        return File(document, "application/octet-stream");
    }
Pang
  • 9,564
  • 146
  • 81
  • 122
Jays
  • 90
  • 8
  • 1
    Hi, only upon setting the content type to 'application/pdf' instead of 'application/octet-stream', i got it working. Thanks for your suggestion. – jothi Oct 24 '16 at 03:57