How to upload files in .NET programing?
General things
The first version of Microsoft ASP.NET was 1.0. Since then the team worked for introducing possibilities to create web applications that had the ability to upload files to the hosting server. This was done through the use of the FileField HTML server control. Now we refer to the 2.0 version of ASP.NET.This article try to introduce you to the new FileUpload server control.
FileUpload Server Control Example
If you want to use the FileField control in ASP.NET 1.x version you must know that you had to take a few extra steps to get everything in line and working. In ASP .NET 2.0 version things are ease to be done. The new version of FileUpload server control makes the process of uploading files to the hosting server as simple as possible.
I will show you how to use the FileUpload control to upload files to the server. Here are two examples of using FileUploade: one of them is wrote in Microsoft Visual Basic and the other one in Microsoft Visual C#:
Visual Basic Example:
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
If FileUpload1.HasFile Then
Try
FileUpload1.SaveAs("C:Uploads\" & _
FileUpload1.FileName)
Label1.Text = "File name: " & _
FileUpload1.PostedFile.FileName & "<br>" & _
"File Size: " & _
FileUpload1.PostedFile.ContentLength & " kb<br>" & _
"Content type: " & _
FileUpload1.PostedFile.ContentType
Catch ex As Exception
Label1.Text = "ERROR: " & ex.Message.ToString()
End Try
Else
Label1.Text = "You have not specified a file."
End If
End Sub
</script>
<html >
<head runat="server">
<title>Upload Files</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Upload File" /> <br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
</body>
</html>
Visual C# Example:
<%@ Page Language="C#" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
try
{
FileUpload1.SaveAs("C:Uploads\" +
FileUpload1.FileName);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
else
{
Label1.Text = "You have not specified a file.";
}
}
</script>
<html >
<head runat="server">
<title>Upload Files</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Upload File" /> <br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
</body>
</html>
Running this you will obtain the source code generated from the FileUpload control:
<html >
<head><title>
Upload Files
</title></head>
<body>
<form name="form1" method="post" action="MyFileUpload.aspx"
id="form1" enctype="multipart/form-data">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNDcxNTg5NDg3D2QWAgIEDxYCHgdlbmN0eXBlBRNtdWx0aXBhcnQvZm9yb
S1kYXRhZGQUQEUFMY1+/fp1mnrkbqmVNQIzFA==" />
</div>
<div>
<input type="file" name="FileUpload1" id="FileUpload1" /><br />
<br />
<input type="submit" name="Button1" value="Upload File"
id="Button1" /> <br />
<br />
<span id="Label1"></span>
</div>
<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"
value="/wEWAgLB+7jIAwKM54rGBv2Iz6LxVY7jWec0gZMxnuaK2ufq" />
</div></form>
</body>
</html>
As you can see ASP.NET 2.0 modified the page’s<form > element on your behalf by adding the appropriate enctype attribute. Another important thing to remark here is that the FileUpload control was converted to an HTML <input type="file" > element.
If you run the file from the examples you can upload files to the server by clicking on the Upload File button and selecting the file that you want to upload and that’s a very ease job.

RSS/XML