Download File From Server In Asp.Net

In this example i'm explaining how to Download Files From Server In Asp.Net and display Save As dialog box when a hyperlink is clicked on the site.

Download files from server in asp.net

I have created a folder on server and stored some sample files to download.

First we need to write code to display names of all the files saved in Files folder as hyperlinks so that user can download these files.

You can read to know how to Save Files In Sql Database And Download From GridView In Asp.Net

Write below mentioned code in Page_Load event of page where you want to display all the links with file names.



C# CODE
protected void Page_Load(object sender, EventArgs e)
    {
        DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~/Files"));
        int counter = 0;
        foreach (FileInfo file in directory.GetFiles())
        {
            HyperLink link = new HyperLink();
            link.ID = "Link" + counter++;
            link.Text = file.Name;
            link.NavigateUrl = "Default2.aspx?name="+file.Name;
            
            Page.Controls.Add(link);
            Page.Controls.Add(new LiteralControl("<br/>"));
        }
    }

VB.NET CODE
Protected Sub Page_Load(sender As Object, e As EventArgs)
 Dim directory As New DirectoryInfo(Server.MapPath("~/Files"))
 Dim counter As Integer = 0
 For Each file As FileInfo In directory.GetFiles()
  Dim link As New HyperLink()
  link.ID = "Link" & System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
  link.Text = file.Name
  link.NavigateUrl = "Default2.aspx?name=" + file.Name

  Page.Controls.Add(link)
  Page.Controls.Add(New LiteralControl("<br/>"))
 Next
End Sub

Add a new web form Default2.aspx in the soulution and write code mentioned below in Page_Load Event.

C# CODE
protected void Page_Load(object sender, EventArgs e)
    {
        string fileName = Request.QueryString["name"].ToString();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.TransmitFile(Server.MapPath("~/Files/" + fileName));
        Response.End();
    }

VB.NET CODE
Protected Sub Page_Load(sender As Object, e As EventArgs)
 Dim fileName As String = Request.QueryString("name").ToString()
 Response.ContentType = "application/octet-stream"
 Response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)
 Response.TransmitFile(Server.MapPath("~/Files/" & fileName))
 Response.[End]()
End Sub

Build and run the code.

Download Sample Code


If you like this post than join us or share

5 comments:

Anonymous said...

This comment has been removed by a blog administrator.


sathyan said...

Thank you very much for the this code. Could you please let us know how to go about when there are multiple sub folders inside the main folder. Would it be possible to display the sub folders and navigate through them to download the files inside them.


Unknown said...

@Sathyan: Please refer Display Files and Directories from server


Unknown said...

hello

i want to move attached file path into database, and into another page as hyperlink to open it , how can i do that ???


Angelo said...

Try this example:

public void TheDownload(string path)
{
System.IO.FileInfo toDownload = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(path));

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition",
"attachment; filename=" + toDownload.Name);
HttpContext.Current.Response.AddHeader("Content-Length",
toDownload.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(patch);
HttpContext.Current.Response.End();
}
The implementation is done in the follows:

TheDownload("@"c:\Temporal\Test.txt"");

Source: http://www.systemdeveloper.info/2014/03/force-downloading-file-from-c.html


Find More Articles