First of all, we need a HTML server control to allow the user to select the file. This is nothing but the same old input tag, with the type set to File, such as
<input type=file id=myFile runat=server />
This will give you the textbox and a browse button. Once you have this, the user can select any file from their computer (or even from a network). Then, in the Server side, we need the following line to save the file to the Web Server.
myFile.PostedFile.SaveAs("DestinationPath");
Code:
<html>
<head>
<script language="VB" runat="server">
Sub Upload(Source As Object, e As EventArgs)
If Not (myFile.PostedFile Is Nothing) Then
Dim intFileNameLength as Integer
Dim strFileNamePath as String
Dim strFileNameOnly as String
'Logic to find the FileName (excluding the path)
strFileNamePath = MyFile.PostedFile.FileName
intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
myFile.PostedFile.SaveAs("c:\inetpub\wwwroot\yourwebapp\upload\" & strFileNameOnly)
lblMsg.Text = "File Upload Success."
End If
End Sub
</script>
</head>
<body>
<h3>File Upload</h3>
<form enctype="multipart/form-data" runat="server">
File: <input id="myFile" type="file" runat="server">
<asp:label id=lblMsg runat="server" />
<input type=button value="Upload" OnServerClick="Upload" runat="server">
</form>
</body>
</html>