In ASP.net 1.0/1.1, you can upload files using the HTML file Upload server control. This control enables an <input type= the content on the "file" > Web page so that end users upload files to the server. To use the file, however, you have to make a couple of modifications to the page. For example, you must add the character encoding = <form> element of the Multiple/form data page.
ASP.net 2.0 introduces a new file Upload server control, which makes the process of uploading files to the server simpler. When giving a page the ability to upload files, you only need to include new <asp:FileUpload> control and ASP.net to take care of the rest, including adding enctype attributes to the <form> elements of the page.
Uploading files using the FileUpload control
After the file is uploaded to the server, you can also grasp the properties of the uploaded file, or display it to the end user or use the code of your page to hide these values. Listing 1 shows an example of using a new FileUpload control. The page contains a FileUpload control, as well as a button and a Label control.
Vb
<%@ 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>
<title>fileupload Server control</title>
<body>
<form id= "Form1" runat= "Server" >
<asp:fileupload id= "FileUpload1" runat= "Server"/>
<p>
<asp:button id= "Button1" runat= "Server" text= "Upload"
onclick= "Button1_Click"/></p>
<p>
<asp:label id= "Label1" runat= "Server" ></asp:Label></p>
</form>
</body>
<%@ 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>
As you can see from this example, the whole process is very simple. A button on the page initiates the upload process. The FileUpload control itself does not have an active upload process. You must take the initiative to pass other events such as Button_Click.
When compiling and running this page, you may notice several things about the source code generated in the Web page. An example of a generated source code is presented here:
fileupload Server control
</title> <body>
<form name= "Form1" method= " Post "action=" fileupload.aspx "id=" Form1 "
enctype=" Multipart/form-data "
< Div>
<input type= "hidden" name= "__viewstate" id= "__viewstate"
value= "/ WEPDWUKMTI3ODM5MZQ0MG9KFGICAW8WAH4HZW5JDHLWZQUTBXVSDGLWYXJ0L2ZVCM
0tzgf0ywrkrspgafaeked5+5/8+ Zkglffvlce= "/>
</div>
<input type=" file "Name=" FileUpload1 "id=" FileUpload1 "/>
<p>
<input type= "Submit" Name= "Button1" value= "Upload" id= "Button1"/> </p>
<p>
<span id= "Label1" ></SPAN></P>
<div>
<input type= "hidden" name= "__eventvalidation" id= "__eventvalidation"
Value= "/wewagl1wlwicakm54rgbqfr8mhzidwvowox+tuvybg5xj0y"/>
</div></form>
</body>
As you can see from this example, the whole process is very simple. A button on the page initiates the upload process. The FileUpload control itself does not have an active upload process. You must take the initiative to pass other events such as Button_Click.
When compiling and running this page, you may notice several things about the source code generated in the Web page. An example of a generated source code is presented here: