. Net C # multi-file upload function design

Source: Internet
Author: User

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

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.