views:

19

answers:

1

I have a file, say a PDF on my website and when a user visits a page I want to display a download dialog for the pdf on page load or a button click.

I did a google search and I found two ways to do this but wondering what is the accepted way of doing this? I am currently doing this

string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";


Response.AppendHeader( "content-disposition",
        "attachment; filename=" + name );
Response.WriteFile(pdfPath);
Response.End();

(Code was based off code from http://aspalliance.com/259, also found code from http://www.west-wind.com/weblog/posts/76293.aspx)

+1  A: 

Your code, will display the file to the user perfectly. But they will have to use the "Save As" option to actually save it.

If you wish to present the "Save Dialog" to the user, try the following:

string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";
Response.AppendHeader("content-disposition",
        "attachment; filename=" + pdfPath );
Response.TransmitFile(pdfPath);
Response.End();

This of course assumes the file actually exists on the server and is not being dynamically generated.

Mick Walker