Download files from IIS Server using ASP.Net

This code helps you to download files from IIS to client computer using ASP.Net. This code can be used for sites where users can to download files by clicking a link or button.

'Getting File name to download using Querystring
Dim objFile As New FileInfo(Path.Combine(Server.MapPath("photos"),Request.QueryString("file")))
Response.Clear()
'Adding header, this says to the client browser that which file to download More info about this header :http://www.ietf.org/rfc/rfc2183.
Response.AddHeader("Content-Disposition", "attachment; filename="& objFile.Name)
Response.AddHeader("Content-Length", objFile.Length)
Response.WriteFile(objFile.Name)
Response.End()

And you can use the code like this

Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Server.Execute("download.aspx?file="&Filename)
End Sub
This entry was posted in .Net. Bookmark the permalink.

One Response to Download files from IIS Server using ASP.Net

  1. Pingback: .NET Thoughts » How to Store and Retrieve files from SQL Server Database

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>