Implementation of file upload in asp.net 2.0

Source: Internet
Author: User
Tags file size file upload html form

Since the introduction of Microsoft asp.net version 1.0, there is a built-in way to build WEB applications that can upload files to the host server. This is accomplished by using the File Field HTML server control. I've written an MSDN article about how to effectively use the control in an ASP.net application. This article will again describe the file upload process, but instead of using the Files Field control, I'll show you how to effectively use the new FileUpload server control provided by ASP.net 2.0.

Although this article introduces you to the new FileUpload server control, it is important to note that you can still use the File Field control in your application.

FileUpload server Control Sample

When you use the File Field control in asp.net 1.x, you must take some extra steps to keep everything running smoothly. For example, you need to add enctype= "Multipart/form-data" to the <form> element of the page yourself. The new FileUpload server control provided in ASP.net 2.0 makes it as simple as possible to upload files to the host server.

Finally, you tried to allow programming of the HTML <input type= "file" > tag. This tag is used with file data in an HTML form. In the past, when traditional ASP (ASP 3.0 or earlier) was used, many programmers used Third-party components to upload files from the client to the server. It is now possible to upload with. NET and the new control. Listing 1 shows how to use the FileUpload control to upload files to the server.

Note provides sample code in the form of Microsoft Visual Basic and C #.

Listing 1. Uploading files to a server using the FileUpload control

Visual Basic

<%@ 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 = "Erro R: "& Ex." Message.tostring ()
End Try
Else
Label1.Text = ' You have not specified a file. '
End If
End Sub
</script>
<title>upload files</title>
<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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.