In this article I will explain with an example, how to download file from Folder (Directory) in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of:
Button – To download the File.
The Button has been assigned with an OnClick event handler.
<asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="Download" />
 
 
Downloading the File
When the Download Button is clicked, the following event handler is executed.
Inside this event handler, the Response class properties are set.
 
Properties:
1. ContentType – It informs the Browser about the file type. In this case it is PDF.
2. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
Once the Response properties are set, the PDF file contents are read.
Finally, using the WriteFile method of the Response class, the File is written to the Response using the Path of the PDF file and the file is downloaded.
C#
protected void Download(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment; filename=Sample.pdf");
    Response.WriteFile(Server.MapPath("~/Sample.pdf"));
    Response.Flush();
    Response.End();
}
 
VB.Net
Protected Sub Download(ByVal sender As Object, ByVal e As EventArgs)
    Response.Clear()
    Response.Buffer = True
    Response.Charset = ""
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment; filename=Sample.pdf")
    Response.WriteFile(Server.MapPath("~/Sample.pdf"))
    Response.Flush()
    Response.End()
End Sub
 
 
Screenshot
Download File in ASP.Net using C# and VB.Net
 
 
View Demo
 
 
Downloads