Uploading files to the server using ASP.net

Source: Internet
Author: User
Tags file size file upload save file split
asp.net| Server | Upload file Upload technology is a very practical technology, has a very wide range of applications, In ASP.net's own previous version of the ASP to implement this function, you must use Third-party components or the development of their own components, now, the implementation of the asp.net is much simpler, we do not need to use any components to achieve the upload function.
To facilitate the explanation, we can upload the file into two types: Single file upload and multiple file upload.

Single File Upload
Let's first introduce a single file upload method, single file upload is relatively simple,
Here is the complete code to implement a single file upload:
<%@ Import namespace= "System"%>
<%@ Import namespace= "System.Web.HttpPostedFile"%>
<%@ Import namespace= "System.Web.UI.HtmlControls.HtmlInputFile"%>
<script language= "VB" runat= "Server" >
Sub UpLoad (Src as Object, E as EventArgs)
If Uploadfile.postedfile.contentlength=0 Then
showuploadfile.innertext= "Upload failed or file does not exist!" "
Else
' Get filename
Dim Temp () as String=split (UploadFile.PostedFile.FileName, "\")
Dim FileName As String=temp (temp.length-1)
' Save file
UploadFile.PostedFile.SaveAs (Server.MapPath (".") & "\files\" & FileName)
' Show upload results
Showuploadfile.innerhtml= "File Upload success!" <br> upload filename: "& filename
End If
End Sub
</script>
<body>
<form runat= "Server" enctype= "Multipart/form-data" >
<input type= "File" id= "UploadFile" runat= "Server" size= "M" ><br>
<asp:button runat= "Server" text= "Upload Now"/>
</form>
<span id= "Showuploadfile" runat= "Server" ></span>
</body>
Save the above code as an. aspx file, and then create a new directory in the directory where the file is located. Files, run, feel the effect, and then continue to read the following
Uploading files using ASP.net requires two classes of the. NET Framework: Httppostedfile and HtmlInputFile, where the namespaces are System.Web.HttpPostedFile and SYSTEM.WEB.UI.HTMLCONTR Ols. HtmlInputFile, so we're going to import these two namespaces at the beginning of the file,
The PostedFile represents the file uploaded to the server, which contains several commonly used properties:
ContentLength: File size;
FileName: Upload the file details of the path and filename;
ContentType: File type for uploading files.
Character Split function split is used to get the filename, because the postedfile.filename gets the detailed path and filename.

Multiple file uploads
The so-called multi-file upload is to upload multiple files at the same time, this is the same as a single file upload is the same, the difference is that multiple files upload all files as a collection of files uploaded to the server, we need to be the collection of this file into a single file, The rest of the processing is the same as a single file upload.
First you need to know how many files to upload at the same time, and then you put the following Htmlinput controls between the form:
<input type= "File" runat= "server" size= "M" >
Note: Here the Htmlinput control control is not required to set the ID
How do I get a file out of a collection of files uploaded to the server? Look at the following code:
Dim i As Integer
For I=0 to Request.files.count-1
' Use Request.Files () to get uploaded files one by one
Dim myFile As Httppostedfile=request.files (i)
' Here the myfile is equivalent to the example in the PostedFile, you can use Myfile.filename to get the file name, etc
' The processing code here is the same as a single file upload.
Next
Now you have mastered the ASP.net file upload technology, as long as flexible application, beautify upload interface, make good asp.net upload program is not difficult.


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.