Reputation: 1654
I am making a web site in asp.net with c sharp. I need to place a download file functionality(pdf, doc, xls) on one of my web page.
How can I do that?
Upvotes: 0
Views: 1075
Reputation: 6890
If you want to do this automatically when a link is clicked from the server side, you have to send the file back yourself rather and add a couple of custom headers to the output. The way to do this is to use Response.TransmitFile()
to explicitly send the file from your ASP.NET application and then add the Content Type and Content-Disposition headers.
For example:
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition","attachment; filename=someFIle.xls");
Response.TransmitFile( Server.MapPath("~/somewhere/someFIle.xls") );
Response.End();
This will cause a Open / Save As dialog box to pop up with the filename of someFIle.xls
as the default filename preset.
Upvotes: 2
Reputation: 47375
To force downloads, you have to set a couple of http headers. Content-Type and Content-Disposition. The first has to be application/octet-stream, and the second has to look something like this:
Content-Disposition: Attachment; Filename="[path to file user wants to download]"
Upvotes: 0