In my previous BLog,we have seen uploading file to the Webserver,
Let us discuss today on Download.
To Download a file the steps we need to do are
1.Create a listbox and name it as "Lstfiles"
2.Create a Button control and name it as "btndownload"
In the Code behind Page.
1.Imports System.data.IO (to do file operations)
2.Imports System.Data Namespaces
3.In the page load event bind the list of files in the folder to the Listbox.
We can bind the data using GetFiles Method of Directory Class
Here I am getting the list of files from the "UploadDocs" folder from the server
Dim i As Int16
'files is an array that stores list of files in the dirctory mentioned
Dim files() As String
'Server.MapPath("UploadDocs") get address of the folder on the server machine
files = Directory.GetFiles(Server.MapPath("UploadDocs"))
For i = 0 To files.Length - 1
files(i) = New FileInfo(files(i)).Name
Next
'Bind the array to Listbox
If Not Page.IsPostBack Then
lstfiles.DataSource = files
lstfiles.DataBind()
End If
4.In btndownload Click event stream the selected file from the listbox to Response Object.
The code behind code would be as follows..
Imports System.IO
Imports System.Data
Partial Class FileDownload
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim i As Int16
'files is an array that stores list of files in the dirctory mentioned
Dim files() As String
'Server.MapPath("UploadDocs") get address of the folder on the server machine
files = Directory.GetFiles(Server.MapPath("UploadDocs"))
For i = 0 To files.Length - 1
files(i) = New FileInfo(files(i)).Name
Next
'Bind the array to Listbox
If Not Page.IsPostBack Then
lstfiles.DataSource = files
lstfiles.DataBind()
End If
End Sub
Protected Sub btndownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndownload.Click
Dim f1 As FileInfo
Dim Selfile As String
Selfile = Server.MapPath("UploadDocs") & "\" & lstfiles.SelectedItem.Text
f1 = New FileInfo(Selfile)
'Clear the buffer,if it not cleared there is a chance of OverWriting the file and file gets Corrupted
Response.Clear()
'Add ContentDisposition and Content Length,Content type to header file
Response.AddHeader("Content-Disposition", "attachment;filename=" & lstfiles.SelectedItem.Text)
Response.AddHeader("Contetn-Length", f1.Length)
Response.ContentType = "application/octet-stream"
Response.Write(Selfile)
Response.End()
End Sub
End Class
To stream a file to a browser, you must write it to the Response object. The first step in writing a file to the Response object is to call its Clear method to remove any data currently in the buffer stream. If the Response object already contains data, when you attempt to write a file to it, you will receive a corrupt file error.
Before writing the file, use the AddHeader method of the Response object to add the name of the file being downloaded and its length to the output stream. You must also use the ContentType method to specify the content type of the file. In this example, the type is set to application/octet-stream so that the browser will treat the output stream as a binary stream and prompt the user to select a location to which to save the file. In your own application you may want to set the content type to an explicit file type, such as application/PDF or application/msword. Setting the content type to the explicit file type allows the browser to open it with the application defined to handle the specified file type on the client machine.
Now, at last, you are ready to write the file to the Response object using Response.WriteFile. When the operation is complete, call Response.End to send the file to the browser.
Any code that appears after the Response.End statement will not be executed. The Response.End statement sends all buffered data in the Response object to the client, stops the execution of the page, and raises the Application_EndRequest event
No comments:
Post a Comment