The final result is as follows:
The core code is as follows:
In aspx Code :
Copy code The 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 code The 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 code The 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