ASP. NET 2.0 File Upload Control
ASP. NET 2.0 File Upload Control
In ASP. NET 1.0/1.1, you can use the HTML file upload server to upload files. This control allows an <input type = "file"> webpage to upload files to the server. To use this file, however, you must modify the couple to the page. For example, you must add the <form> element on the "multi-factor/form data" page.
ASP. NET 2.0 introduces a new file upload server control, making the process of uploading files to the server easier. To upload files to a page, you only need to include the new <asp: FileUpload> Control and ASP. NET takes into account the rest, including the <form> element of the page that is added to the enctype attribute.
Use the FileUpload control to upload files
After the file is uploaded to the server, you can also grasp the attributes of the uploaded file, either display it to the end user or use your webpage code to hide these values. Listing 1 shows an example of using the new FileUpload control. The webpage contains a FileUpload control, a button, and a Label control.
VB
<% @ Page Language = "VB" %>
<Script runat = "server">
Protected Sub
Button#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 xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> FileUpload Server Control </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: FileUpload ID = "FileUpload1" runat = "server"/>
<P>
<Asp: Button ID = "Button1" runat = "server" Text = "Upload"
OnClick = "button#click"/> </p>
<P>
<Asp: Label ID = "Label1" runat = "server"> </asp: Label> </p>
</Form>
</Body>
</Html> C #
<% @ Page Language = "C #" %>
<Script runat = "server">
Protected void button#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>
From this example, we can see that the entire process is very simple. Click a button on the page to start the upload process. The FileUpload control does not take the initiative to upload data. You must take the initiative to pass other events such as Button_Click.
When you compile and run this webpage, you may notice the source code generated on the webpage. An example of the generated source code is presented here:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1"> <title>
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 = "/delimiter
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>
</Html>
From this example, we can see that the entire process is very simple. Click a button on the page to start the upload process. The FileUpload control does not take the initiative to upload data. You must take the initiative to pass other events such as Button_Click.
When you compile and run this webpage, you may notice the source code generated on the webpage. An example of the generated source code is presented here: