The resource has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this resource. This resource will be reviewed shortly. If you think this resource contain inappropriate content, please report to webmaster. |
Force Download ===============
Introduction Download a file from server to client is simple, just by refrencing the file path all alone can download the specified file from the server. But a force download is some what tricky and lot more different. Let's see how to force download .
Sample
''''''''''''''''code done by Harmanpreet Singh, final year CSE student '''''''''''''''''''' ''''''Subroutine is being used to download files from server''''''' '''''_strFlpath is the physical file path '''''_strFlName is the file name (display while download) Sub subDownload(ByVal _strFlPath As String, ByVal _strFlName As String) Try Dim path As IO.Path Dim fullpath = path.GetFullPath(_strFlPath) 'Dim name = path.GetFileName(fullpath) Dim name = _strFlName Dim ext = path.GetExtension(fullpath) Dim type As String = "" If Not IsDBNull(ext) Then ext = LCase(ext) End If Response.Clear() Response.ClearContent() Response.ClearHeaders() Select Case ext Case ".htm", ".html" type = "text/HTML" Case ".txt" type = "text/plain" Case ".doc", ".rtf" type = "Application/msword" Case ".csv", ".xls" type = "Application/x-msexcel" Case ".mdb" type = "Application/vnd.msaccess" Case ".mp3", ".mpeg", ".mp2", ".wav", ".avi", ".dat", ".jpg", ".swf", ".jpeg", ".bmp" type = "application/octet-stream" Case ".pdf" type = "Application/pdf" Case ".exe" type = "application/octet-stream" Case Else type = "text/plain" End Select Dim sourceFile As IO.FileStream sourceFile = IO.File.Open(fullpath, IO.FileMode.Open) Dim getContent(sourceFile.Length) As Byte sourceFile.Read(getContent, 0, sourceFile.Length) sourceFile.Close() Response.AddHeader("content-disposition", "attachment; filename=" + name) If type <> "" Then Response.ContentType = type End If Response.BinaryWrite(getContent) Response.End() Catch ex As Exception lblErrMsg.Text = ex.Message lblErrMsg.Visible = True End Try End Sub
How this code works To force download files from server, we need to use response object. keep in mind, we need to clear response object when ever we start our force download.(reason: if not we will get the garbage values in our downloaded files.). Here in this subroutine which receives the physical file path (ie the server.mappath), filename (which is to show while downloading. You may have file with named xxxx.xxx. when u pass filename as yyyy.yyy then ur download popup will display yyyy.yyy instead of xxxx.xxx.) This subroutine which covers most of the known extensions. For unknown extension, plain content type is used.Response.Binarywrite will initiate download with open-save-cancel popup.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|