NET implementation of multiple file upload code

Source: Internet
Author: User
Tags datetime rar zip

NET to achieve multiple file upload code, a single file upload may be easy to do, but many friends is not multiple file upload, other multiple file upload and single file is not the difference in nature, so we first look at the following article to know.

protected bool Upmorefile ()
{

Traversing file Form elements
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
Status information
System.Text.StringBuilder STRMSG = new System.Text.StringBuilder ("Successfully uploaded file information is: int filecount;
int filecount = files. Count;


Try
{
for (filecount = 0; filecount < files. Count; filecount++)
{
Define an object to access the client upload file
System.Web.HttpPostedFile postedfile = Files[filecount];
String FileType = PostedFile.ContentType.ToString ()//Get file type to upload, verify file header

String FileName, FileExtension;
Get uploaded file name
FileName = System.IO.Path.GetFileName (postedfile.filename);
To get the file name extension
FileExtension = System.IO.Path.GetExtension (fileName);

Verify that the file name and size are compliant if the upload files are not empty, and do not allow uploads if they are not met
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)
{//Here the file upload type is restricted by checking to see if the file header matches the file name Note: The type that can be uploaded has Txt,zip,rar, and the size can only be 1M

if (fileName!= String.Empty)
{
FileName = Randomfilename () + fileextension;

Uploaded file information
Strmsg.append ("uploaded file type:" + postedFile.ContentType.ToString () + "<br>");
Strmsg.append ("Client file Address:" + postedfile.filename + "<br>");
Strmsg.append ("FileName of uploaded file:" + filename + "<br>");
Strmsg.append ("Upload file size is:" + postedfile.contentlength + "byte <br>");
Strmsg.append ("Upload file extension:" + fileextension + "<br>Save to the specified folder
Postedfile.saveas (Server.MapPath ("public_file/" + UserName + "/") + FileName);
FileName = "";

}
}
Else
{
strstatus.text+= "No." + (filecount+1) + "a document does not meet the requirements <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 = "";//Prompt information empty
Upmorefile ()//Call upload class

}

public string Randomfilename ()
{//Returns a class of random numbers
string filename = "";
string r1 = "";
string r2 = "";
string r4 = "";
Random Random = new Random ();
R1 = ((char) random. Next (65, 90)). ToString ();//Capital Letter
r2 = ((char) random. Next (97, 122)). ToString ();//Small Letter
R4 = random. Next (10000, 999999). 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 foreground code is as follows

<script language= "JavaScript" >
function Addfilecontrol ()
{
var str = ' <input type= ' file ' name= ' file ' > '
document.getElementById (' filecollection '). 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" >
The types that are allowed to upload are: zip,rar,txt, size 1M below </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= "Song Body" ></asp:label></P>

</asp:Panel>
And be sure to add a enctype entry in the form before you can use it. As follows:

<form id= "Form1" runat= "Server" enctype= "Multipart/form-data"

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.