Tuesday, December 14, 2010

Download File Dialog box

AS

Code to open a "save as.." file download dialog box in asp.net 2.
Here,I create a vb.net function for to open file download dialog box.

Use System.IO class for file system. Function argument accepts a File virtual path not physical path.

This code for only .txt file but you also use this code for more extension file.For that change only ContentType.

.htm,.html => "text/HTML"
.txt => "text/plain"
.doc,.rtf => "Application/msword"
.csv,.xls => "Application/x-msexcel"
.pdf =>"Application/pdf"


Function DisplayDownloadDialog(ByVal PathVirtual As String)

Dim strPhysicalPath As String
Dim objFileInfo As System.IO.FileInfo
Try
strPhysicalPath = Server.MapPath(PathVirtual)
'exit if file does not exist
If Not System.IO.File.Exists(strPhysicalPath) _
Then Exit Function
objFileInfo = New System.IO.FileInfo(strPhysicalPath)

Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
'Add Headers to enable dialog display
Response.AddHeader("Content-Disposition", "attachment; filename=" & _
objFileInfo.Name)
Response.AddHeader("Content-Length", objFileInfo.Length.ToString())

Response.ContentType = "Text / plain"

Response.WriteFile(objFileInfo.FullName)


Catch
'on exception take no action
'you can implement differently
Finally

Response.End()

End Try
End Function

No comments:

Post a Comment