Displaying Files

  • Thread starter Thread starter MichaelR
  • Start date Start date
M

MichaelR

I have an aspnet application in which I would like users to be able to
display certain files - they are pdf files.

For security reasons, these files are located in a directory that is not
available to my users as a url.

What I've tried doing is building a .aspx page that has access to the file
directory, opens the file as a binary stream, read it into a buffer and then
do a binary write into the Response object. I also set the
Response.ContentType to the proper mime type (e.g., application/pdf).

The problem I run into is that the end user's browser sees this file as the
name of the aspx page (e.g., showfile.aspx) rather than the pdf file that it
is (e.g., sample.pdf).

Is there some way to set this file name in the Response object so that their
browsers will handle it properly? I've tried setting
Response.RedirectLocation, but that didn't work.

Is there a better way to let users download these files?

Thanks.
 
Something along these lines should do it:

Response.ContentType = "application/PDF";
Response.AppendHeader("content-disposition", "attachment:
filename=test.pdf");
Response.WriteFile("c:\test.pdf");

Here is a more complex approach, a bit closer to what you are currently
doing:
http://steve.orr.net/content/asp200307so_f.asp
 
Back
Top