Monday, 10 March 2014

How to Upload files in ASP .NET

How to Upload files in ASP .NET
Use input file control.
Create a aspx page. Add below code.
<input type=file id=myFile runat=server />
Go to server side code of that page.
myFile.PostedFile.SaveAs("c:\")
Sample code
----------
<html>
<head>
<script language="VB" runat="server">
Sub FileUpload(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:\" & strFileNameOnly)
lblMsg.Text = "File Upload Successfully."
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="FileUpload" OnServerClick="FileUpload" runat="server">
</form>
</body>
</html>