Asp.net multi-File Upload, compatible with IE6/7/8, provides complete code download

Source: Internet
Author: User

The final result is as follows:

The core code is as follows:
Code in aspx:
Copy codeThe Code is as follows:
<Div style = "text-align: center">
<Div style = "width: 200px;">
<Input type = "file" size = "50" name = "File"/>
<Span id = "upload"> </span>
<Br/>
<Input type = "button" name = "button" value = "add file" onclick = "addInput ()">
<Input type = "button" name = "button" value = "delete file" onclick = "deleteInput ()">
</Div>
<Div style = "margin: 10px 0 10px 0; width: 200px;">
<Asp: Button runat = "server" Text = "Upload" ID = "btnUpload" OnClick = "btnUpload_Click"> </asp: Button> <br/>
<Asp: Label ID = "strStatus" runat = "server"> </asp: Label>
</Div>
</Div>

Javascript is called to add or delete files. The Code is as follows:

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var attachname = "uploadfile ";
Var I = 1;
Function addInput (){
If (I> 0 ){
Var attach = attachname + I;
If (createInput (attach ))
I = I + 1;
}
}
Function deleteInput (){
If (I> 1 ){
I = I-1;
If (! RemoveInput ())
I = I + 1;
}
}
Function createInput (nm ){
Var aElement = document. createElement ("input ");
AElement. name = nm;
AElement. id = nm;
AElement. type = "file ";
AElement. size = "50 ";
If (document. getElementById ("upload"). appendChild (aElement) = null)
Return false;
Return true;
}
Function removeInput (nm ){
Var aElement = document. getElementById ("upload ");
If (aElement. removeChild (aElement. lastChild) = null)
Return false;
Return true;
}
</Script>



The background responds to the operation of saving files. The key sentence for saving files is to read the file list,
// Traverse the File form Element
HttpFileCollection files = HttpContext. Current. Request. Files;
The code for saving files after upload is as follows:
Copy codeThe Code is as follows:
Protected void btnUpload_Click (object sender, EventArgs e)
{
// Traverse the File form Element
HttpFileCollection files = HttpContext. Current. Request. Files;
System. Text. StringBuilder strMsg = new StringBuilder ("<br/> ");
StrMsg. Append ("the uploaded files are: </br> ");
Try
{
For (int iFile = 0; iFile <files. Count; iFile ++)
{
// Check the file extension name
HttpPostedFile postedFile = files [iFile];
String fileName, fileExtension;
FileName = System. IO. Path. GetFileName (postedFile. FileName );
If (fileName! = "")
{
FileExtension = System. IO. Path. GetExtension (fileName );
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 ("File Upload Extension:" + fileExtension + "<br/> ");
StrMsg. Append ("Size of the uploaded file:" + postedFile. ContentLength + "<br/> ");
// Scalability:
// You can set the storage directory when saving the file.
// Rename the file and save it
PostedFile. SaveAs (System. Web. HttpContext. Current. Request. MapPath ("images/") + fileName );
}
}
StrStatus. Text = strMsg. ToString ();
}
Catch (System. Exception Ex)
{
StrStatus. Text = Ex. Message;
}
}


Download complete code

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.