|
|
|
Many times we have requirement to popup file download dialogbox instead of user clicking on a link. This little code you can use in your ASPX page. Call the following method from your ASP.net code which should prompt a file download dialogbox in user's browser. |
Click here to copy the following block | Public Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean) Dim path As System.IO.Path Dim strMSG As String Dim fullpath = fname Dim name = path.GetFileName(fullpath) Dim ext = path.GetExtension(fullpath) Dim type As String = ""
If Not IsDBNull(ext) Then ext = LCase(ext) End If
Select Case ext Case "doc" type = "application/ms-word" Case "dot" type = "application/ms-word" Case "exe" type = "application/octet-stream" Case "xls" type = "application/ms-excel" Case "ppt" type = "application/ms-powerpoint" Case "pps" type = "application/ms-powerpoint" Case "txt" type = "text/plain" Case "htm" type = "text/html" Case "html" type = "text/html" Case "pdf" type = "application/pdf" Case "rtf" type = "application/rtf" Case "zip" type = "application/x-zip-compressed" Case Else type = "application/unknown" End Select Try System.Web.HttpContext.Current.Response.Clear() System.Web.HttpContext.Current.Response.ClearContent() System.Web.HttpContext.Current.Response.ClearHeaders()
If type <> "" Then System.Web.HttpContext.Current.Response.ContentType = type End If
If (forceDownload) Then System.Web.HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + name) Else System.Web.HttpContext.Current.Response.AppendHeader("content-disposition", "inline; filename=" + name) End If
System.Web.HttpContext.Current.Response.WriteFile(fullpath) System.Web.HttpContext.Current.Response.End() Catch ex As Exception strMSG = "File can't be downloaded at this time. Please try again later." End Try End Sub |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|