For Web Applications Program The file upload function cannot be used during development. However, it was very troublesome to upload files before. Net now makes File Upload easy. The following example implements the Multifile upload function. You can dynamically add input forms. There is no limit on the number of uploaded files. Code As follows:
<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Multifileupload. aspx. CS "
Inherits = " Multifileupload " %>
<! Doctype HTML public"-// W3C // dtd xhtml 1.0 transitional // en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns ="Http://www.w3.org/1999/xhtml">
<Head runat ="Server">
<Title> ASP. NET multi-file upload test </title>
<SCRIPT type =" Text/JavaScript " >
Function AddFile (){
VaR DIV = Document. createelement ( " Div " );
VaR F = Document. createelement ( " Input " );
F. setattribute ( " Type " , " File " )
F. setattribute ( " Name " , " File " )
F. setattribute ( " Size " ," 50 " )
Div. appendchild (f)
VaR d = Document. createelement ( " Input " );
D. setattribute ( " Type " , " Button " )
D. setattribute ( " Onclick " , " Detefile (this) " );
D. setattribute ( " Value " , " Remove " )
Div. appendchild (d)
Document. getelementbyid ( " _ Container " ). Appendchild (DIV );
}
Function detefile (o ){
While(O. tagname! ="Div") O = O. parentnode;
O. parentnode. removechild (O );
}
</SCRIPT>
</Head>
<Body>
<Form ID = " Form1 " Runat = " Server " Method = " Post " Enctype = " Multipart/form-Data " >
<H3> Multifile upload Username: <asp: textbox id = " Textbox1 " Runat = " Server " > </ASP: textbox>
<Div id = " _ Container " >
<Input type = " File " Size = " 50 " Name =" File " />
</Div>
<Div>
<Input type = " Button " Value = " Add) " Onclick = " AddFile () " />
</Div>
<Div style = " Padding: 10px 0 " >
<Asp: button runat = " Server " TEXT = " Start upload " Id = " Uploadbutton "
Onclick =" Uploadbutton_click " > </ASP: button>
</Div>
<Div>
<Asp: Label id = " Strstatus " Runat = " Server " Font-names = " " Font-bold =" True " Font-size = " 9pt "
Width = " 500px " Borderstyle = " None " Bordercolor = " White " > </ASP: Label>
</Div>
</Form>
</Body>
</Html>
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Public Partial ClassMultifileupload: system. Web. UI. Page
{
Protected VoidUploadbutton_click (ObjectSender, eventargs E)
{
///'Traverse file form elements
Httpfilecollection files = httpcontext. Current. Request. files;
/// 'Status Information
System. Text. stringbuilder strmsg = New System. Text. stringbuilder ( " The user name you entered is: " + Textbox1.text + " <Br/> " );
Strmsg. append ( " The uploaded files are: <HR color = 'red'/> " );
Try
{
For ( Int Ifile = 0 ; Ifile <files. Count; ifile ++)
{
/// 'Check the extension name of the file.
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> <HR> " );
/// 'Different folders can be saved according to different extension names.
/// Note: you may need to modify the anonymous write permission for your folder.
Postedfile. saveas (system. Web. httpcontext. Current. Request. mappath ( " Images/ " ) + Filename );
}
}
Strstatus. Text = strmsg. tostring ();
}
Catch (System. Exception ex)
{
Strstatus. Text = ex. message;
}
}
}