Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

ASP.Net Serve File For Download

Function I use to serve a file on the webserver to the client machine.

   1  
   2     ' sends file to browser for download
   3     Private Sub SendFile(ByVal strPath As System.String, ByVal strSuggestedName As System.String)
   4  
   5        Dim strServerPath As String
   6        Dim objSourceFileInfo As System.IO.FileInfo
   7  
   8        ' convert relative path to path on server machine
   9        strServerPath = Me.Server.MapPath(strPath)
  10  
  11        ' get fileinfo of source file
  12        objSourceFileInfo = New System.IO.FileInfo(strServerPath)
  13  
  14        ' if the file exists
  15        If objSourceFileInfo.Exists Then
  16  
  17           With Me.Response
  18  
  19              ' tell the browser what content type to expect
  20              .ContentType = "application/octet-stream"
  21   
  22              ' tell the browser to save rather than display inline
  23              .AddHeader("Content-Disposition", "attachment; filename=" & strSuggestedName)
  24  
  25              ' tell the browser how big the file is
  26              .AddHeader("Content-Length", objSourceFileInfo.Length.ToString)
  27  
  28              ' send the file to the browser
  29              .WriteFile(objSourceFileInfo.FullName)
  30  
  31              ' make sure response is sent
  32              .Flush()
  33  
  34              ' end response
  35              .End()
  36  
  37           End With
  38  
  39        ' if the file does not exist
  40        Else
  41  
  42           ' show error page
  43           ThrowError("Application Error", "File Missing From Server")
  44  
  45        End If
  46  
  47     End Sub        
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS