how to download file in .net

Function DownloadFile(ByVal strFileName As String)Dim fs As FileStreamDim strContentType As String

Dim strPath = ConfigurationSettings.AppSettings(“DocumentsContainerLocal”)‘Dim strFileName As String = Request.QueryString(“DocumentFile”)

fs = File.Open(strPath & strFileName, FileMode.Open)

Dim bytBytes(fs.Length) As Byte

‘ Write the stream to the byte array

fs.Read(bytBytes, 0, fs.Length)

‘ Close the file stream to release the resource


Response.AddHeader(“Content-disposition”, _

“attachment; filename=” & strFileName)

‘ Next we need to add some header information.

‘ These headers will tell the browser what it needs to do

‘ with the content we’re serving

Response.ContentType = “application/octet-stream”

‘ Now our headers are added, we can serve the content.

‘ To do this, we use the BinaryWrite() method of the server object

‘ This successfully streams our external file to the user,

‘ despite the fact that the file doesn’t exist

‘ anywhere inside the web application


‘ Call Response.End() so that no more

‘ content goes through to the client.


End Function


Ali Raza Zaidi, Microsoft ERP Consultant, Currently working for Business Experts Gulf in United Arab Emirates. Specialized in HR & Payroll, Supply Chain and Financials modules.