Aspx:
<Table>
<Tr valign = "TOP">
<TD> attachment </TD>
<TD>
<Div style = "float: Left; width: 30px">
<Input type = "button" value = "+" onclick = "AddFile ()"/>
</Div>
<Div id = "divfile" style = "float: Left">
<Div>
<Input type = "file" style = "width: 400px" runat = "server" name = "Files">
<Asp: button id = "btnsubmit" runat = "server" text = "Submit" onclick = "btnsubmit_click"/>
</Div>
</Div>
</TD>
</Tr>
</Table>
<SCRIPT type = "text/JavaScript">
Function AddFile ()
{
VaR divfile = Document. getelementbyid ('divfile ');
VaR filecount = divfile. getelementsbytagname ("Div"). length;
If (filecount <5) // make a limit based on the situation
{
VaR STR = '<div> <input type = "file" style = "width: 400px "runat =" server "name =" Files "> </div> ';
Divfile. insertadjacenthtml ("beforeend", STR );
}
Else
{
Alert ("up to five more ");
}
}
Function deletefile (child)
{
VaR divfile = Document. getelementbyid ("divfile ");
Divfile. removechild (child. parentnode );
}
</SCRIPT>
Aspx. CS
Protected void btnsubmit_click (Object sender, eventargs E)
{
Httpfilecollection myfile = httpcontext. Current. Request. files;
Int ifilecount = myfile. count;
Lblshow. Text = ifilecount. tostring ();
For (INT I = 0; I <ifilecount; I ++)
{
Httppostedfile hpfile = myfile [I];
If (hpfile. contentlength! = 0)
{
String filename = path. getfilename (hpfile. filename );
String nowtime = datetime. Now. tostring ("yyyymmddhhmmssff") + I; // to keep the file name unique
// Get extension of file name
String fileextension = system. Io. Path. getextension (filename );
Hpfile. saveas (server. mappath ("~ /UI/upload/") + nowtime + fileextension );
}
}
}
Appendix: javascript insert function:Example of the insertadjacenthtml Method
Previously, the innerhtml and innertext methods were used to add HTML content and text content. Recently, insertadjacenthtml and insertadjacenttext methods were found to be more flexible, you can insert HTML content and text content in a specified place.
Insertadjacenthtml method: insert the HTML Tag statement in the specified place
Prototype:Insertadajcenthtml (swhere, stext)
Parameters:
Swhere: specifies where HTML tag statements are inserted. Four values are available:
1. beforebegin: before the start of the tag
2. afterbegin: inserted after the tag starts Marking
3. beforeend: insert to the end tag of the tag.
4. afterend: insert to the end tag of the tag.
Stext: content to be inserted
More