Net implements Multi-file upload code

Source: Internet
Author: User

. Net implements Multi-file upload code. It may be easy to upload a single file. However, many users do not upload multiple files. Other multi-file uploads are essentially different from single files, let's take a look at the following articles.

Protected bool upMorefile ()
{

// Traverse the File form Element
System. Web. HttpFileCollection files = System. Web. HttpContext. Current. Request. Files;
// Status information
System. Text. StringBuilder strMsg = new System. Text. StringBuilder ("successfully uploaded file information: Int fileCount;
Int filecount = files. Count;


Try
{
For (fileCount = 0; fileCount <files. Count; fileCount ++)
{
// Define the object for accessing the client to upload files
System. Web. HttpPostedFile postedFile = files [fileCount];
String FileType = postedFile. ContentType. ToString (); // obtain the file type to be uploaded and verify the file header.

String fileName, fileExtension;
// Get the uploaded file name
FileName = System. IO. Path. GetFileName (postedFile. FileName );
// Get the file extension
FileExtension = System. IO. Path. GetExtension (fileName );

// When the file to be uploaded is not empty, verify that the file name and size are consistent. If not, upload is not allowed.
If (FileType = "text/plain" & fileExtension. toLower () = ". txt ") | (FileType =" application/x-zip-compressed "& fileExtension. toLower () = ". zip ") | (FileType =" application/octet-stream "& fileExtension. toLower () = ". rar ") & postedFile. contentLength/1024 <= 1024)
{// Check whether the file header matches the file name to limit the File Upload type. Note: The File Upload types include TXT, ZIP, and RAR, and the file size can only be 1 MB.

If (fileName! = String. Empty)
{
FileName = RandomFileName () + fileExtension;

// Upload File Information
StrMsg. Append ("type of the uploaded file:" + postedFile. ContentType. ToString () + "<br> ");
StrMsg. Append ("client file address:" + postedFile. FileName + "<br> ");
StrMsg. Append ("file name to be uploaded:" + fileName + "<br> ");
StrMsg. Append ("the size of the uploaded file is:" + postedFile. ContentLength + "byte <br> ");
StrMsg. Append ("File Upload Extension:" + fileExtension + "<br> // Save to the specified folder
PostedFile. SaveAs (Server. MapPath ("public_file/" + UserName + "/") + fileName );
FileName = "";

}
}
Else
{
StrStatus. Text + = "no." + (fileCount + 1) + "non-conforming files <br/> ";

}

}

StrStatus. Text + = strMsg. ToString ();
Return true;
}
Catch (System. Exception error)
{
StrStatus. Text = error. Message;
Return false;

}
}


Protected void Upload_Click (object sender, EventArgs e)
{
StrStatus. Text = ""; // The prompt message is cleared.
UpMorefile (); // call the upload class

}

Public string RandomFileName ()
{// Class that returns a random number
String filename = "";
String r1 = "";
String r2 = "";
String r4 = "";
Random random = new Random ();
R1 = (char) random. Next (65, 90). ToString (); // uppercase letter
R2 = (char) random. Next (97,122). ToString (); // lowercase letter
R4 = random. Next (10000,999 999). ToString ();
Filename = DateTime. now. year. toString () + DateTime. now. month. toString () + DateTime. now. day. toString () + DateTime. now. hour. toString () + DateTime. now. minute. toString () + DateTime. now. second. toString () + DateTime. now. millisecond. toString () + r1 + r4 + r2 + r1 + r4 + r1;

Return filename;

}

The front-end code is as follows:

<Script language = "JavaScript">
Function addFileControl ()
{
Var str = '<INPUT type = "file" NAME = "File">'
Document. getElementById ('filecollect'). insertAdjacentHTML ("beforeEnd", str)
}
</Script>

<Asp: Panel ID = "PanelFileManage" runat = "server" Width = "100%">

<P id = "FileCollection"> <INPUT type = "file" name = "File"> & nbsp; </P>
<P align = "center" style = "color: Red">
Supported upload types: ZIP, RAR, TXT, and smaller than 1 MB </p>
<P align = "center"> <input onclick = "addFileControl ()" type = "button" value = "add (File)">
<Asp: button id = "Upload" Runat = "server" Text = "Upload" Width = "56px" OnClick = "Upload_Click"> </asp: button>
<Input style = "WIDTH: 56px; HEIGHT: 24px" onclick = "this. form. reset ()" type = "button" value = "reset">
</P>
<P align = "center"> <asp: label id = "strStatus" runat = "server" BorderColor = "White" BorderStyle = "None" Width = "500px"
Font-Size = "9pt" Font-Bold = "True" Font-Names = ""> </asp: label> </P>

</Asp: Panel>
You must add an enctype item to the form to use it. As follows:

<Form id = "form1" runat = "server" enctype = "multipart/form-data">

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.