Monday, April 2, 2007

File DownLoad in asp.net ....

if u have upaloaded file path in dbtable means u can download that file using the following code .....

Put a Link Button in Gridview ....



tr
td align="left"

b Attachment : /b
asp:LinkButton ID="lnkbtnpath" runat="server" CommandName="test" CommandArgument=%#Eval("Filepath")% CausesValidation="false"

DownLoad
/asp:LinkButton
/td
/tr




call that file path in Rowcommand Event of Gridview ......

Protected Sub gvAppealperson_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAppealperson.RowCommand
Dim filepath As String
Dim fileobj As FileInfo

' checking the Linkbutton's commandname is valid or not
If e.CommandName = "test" Then

' getting the commandarugment as path from database ( <%#Eval("Filepath")%>) in a string
filepath = e.CommandArgument

' assigning that string to FileInfo's object
fileobj = New FileInfo(filepath)
Response.Clear()

'Add the header that specifies the default filename for the Download/SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" & fileobj.Name)

'Add the header that specifies the file size, so that the browser can show the download progress
Response.AddHeader("Content-Length", fileobj.Length.ToString())

' Specify that the response is a stream that cannot be read by the client and must be downloaded
Response.ContentType = "application/octet-stream"

' Send the file stream to the client
Response.WriteFile(fileobj.FullName)
Response.Flush()

' Stop execution
Response.End()
End If
End Sub

No comments: