Initiate Download Using ASP.NET and VB.NET

Initiate Download Using ASP.NET and VB.NET
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
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top