ali raza

C#, .Net ,Asp.net and my thoughts

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

fs.Close()

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

Response.BinaryWrite(bytBytes)

‘ Call Response.End() so that no more

‘ content goes through to the client.

Response.End()

End Function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: