This function initiates a download of a file fetched from the server's local system, all through ASP.NET and VB.NET.
1. Private Sub FetchFile(ByVal strFromPath As System.String, ByVal strSendWithName As System.String)
2. Dim strServerAbsPath As String
3. Dim infFile As System.IO.FileInfo
4. ' Change the relative path to the absolute path
5. strServerAbsPath = Me.Server.MapPath(strFromPath)
6. ' Will use to check if the file exists
7. infFile = New System.IO.FileInfo(strServerAbsPath)
8. ' If it does...
9. If infFile.Exists Then
10. With Me.Response
11. ' Send headers
12. .ContentType = "application/octet-stream"
13. .AddHeader("Content-Disposition", "attachment; filename=" & strSendWithName)
14. .AddHeader("Content-Length", infFile.Length.ToString)
15. ' Send the file
16. .WriteFile(infFile.FullName)
17. ' Send and end the response
18. .Flush()
19. .End()
20. End With
21. ' File does not exist
22. Else
23. ThrowError("IO Error", "File Not Found")
24. End If
25. End Sub