Introduction
In most of the web applications now a days, there comes a need when we have to provide a link to download some sample file or file like that as per the requirements. So this tip is to perform this task in the easiest way (which I have found so far, as I am a newbie.
Using the Code
The following code performs the file download process in the simplest way, either from the application's local folder or from some system directory.
The developer has to place these line of codes inside the buttonclick or imagebutton clickevent.
Response.ContentType = "Application/xlsx";
Response.AppendHeader("Content-Disposition", "attachment; filename=filename.xlsx");
Response.TransmitFile(Server.MapPath("~/Templates/filename.xlsx"));
Response.End();
In the first line, we have to mention the content type, I have used it for Excel 2007 files while here, we can use the PDF or image as well.
The second line is just appending the header to the httpResponse
and here we have to mention the filename along with the extension which we want to download.
While in the third line Complete Path where file is placed has to be provided.
In the last line of code, the Response is ending.
Points of Interest
Many code snippets are available on Google, but I found this one pretty interesting and straight forward for any programmer who is a kind of newbie like myself.
Do give me your feedback, as this is my first effort on any forum in my life, either suggestions, corrections or praises. :)