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