0

How to dynamically open file resource in browser using Response call in C# and ASP.NET? I have a file server that stores documentation. I have another server that runs IIS and ASP.NET application. I need to open a pdf document stored on the file server in a browser window. I supply folderPath string as "\\MyFileServer\documentFolder\" and filename string as "doc.pdf". I try to redirect to

Response.Redirect(folderPath + filename, true);

but redirection tries to append my provided location of the resource to current server path. I'm new to ASP.NET so please forgive me my ignorance. Oh, BTW, the site is accessed from android tablets so I need explicitly redirect client browser to a file resource, otherwise android will start downloading the file instead of opening it.

ArtK
  • 1,157
  • 5
  • 17
  • 31

2 Answers2

2

Have a look at Response.AddHeader

You can do something like

Response.AddHeader("content-disposition", "inline; filename=" + filepath + ".pdf");
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
  • Thx for the answer. I tried it but nothing happens. Almost like some execute command was missing after this one. Browser keeps displaying my test.aspx. – ArtK Apr 01 '14 at 15:38
  • @ArtK: Is your filepath includes file extension? if it is remove the extension which added manually. Otherwise it may result `Response.AddHeader("content-disposition", "inline; filename.pdf.pdf")` – huMpty duMpty Apr 01 '14 at 15:39
  • I tested path string as seen by browser by sending it to Response.Write(). When I knew that is was correct I used it in Response.AddHeader(). There is no action when the call gets executed. – ArtK Apr 01 '14 at 16:14
  • @ArtK: Its difficult to say what is happening. Have a look at [this](http://www.aspdotnet-suresh.com/2012/11/aspnet-open-pdf-file-in-web-browser.html) and try – huMpty duMpty Apr 01 '14 at 16:17
2

In a response, write a Header:

Content-Disposition: inline; doc.pdf

See more here

Community
  • 1
  • 1
h_s
  • 94
  • 4
  • Thx for the answer. I tried it but nothing happens. Almost like some execute command was missing after this one. – ArtK Apr 01 '14 at 15:35