<! -- StartFragment --> <% @ Page language = "c #" Codebehind = "UpLoad. aspx. cs" AutoEventWireup = "false" Inherits = "WebPortal. Upload" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> Multifile upload </title>
<Script language = "JavaScript">
Function addFile ()
{
Var str = '<INPUT type = "file" size = "50" NAME = "File">'
Document. getElementById ('myfile'). insertAdjacentHTML ("beforeEnd", str)
}
</Script>
</HEAD>
<Body>
<Form ID = "form1" method = "Post" runat = "server" enctype = "multipart/form-Data">
<Div align = "center">
<H3> Multifile upload <P id = "myfile"> <input type = "file" size = "50" name = "file"> </P>
<P>
<Input type = "button" value = "add" onclick = "AddFile ()">
<Input onclick = "This. Form. Reset ()" type = "button" value = "RESET (reset)">
<Asp: button runat = "server" text = "Start upload" id = "uploadbutton"> </ASP: button>
</P>
<P>
<Asp: Label id = "strstatus" runat = "server" font-names = "" font-bold = "true" font-size = "9pt"
Width = "500px" borderstyle = "NONE" bordercolor = "white"> </ASP: Label>
</P>
</Div>
</Form>
</Body>
</Html>
CS code:
Using System; <br/> using System. collections; <br/> using System. componentModel; <br/> using System. data; <br/> using System. drawing; <br/> using System. web; <br/> using System. web. sessionState; <br/> using System. web. UI; <br/> using System. web. UI. webControls; <br/> using System. web. UI. htmlControls; </p> <p> namespace WebPortal <br/> {<br/> /// <br/> <summary> // summary of UpLoad. <Br/> // Multifile Upload <br/> /// </summary> <p> public class Upload: System. web. UI. page <br/> {<br/> protected System. web. UI. webControls. button UploadButton; <br/> protected System. web. UI. webControls. label strStatus; </p> <p> private void Page_Load (object sender, System. eventArgs e) <br/>{< br/> // place the user code here to initialize the page <br/> if (this. isPostBack) this. saveImages (); <br/>}</p> <p> private Boolean SaveImages () <Br/>{< br/> // 'traverse the File form Element <br/> HttpFileCollection files = HttpContext. current. request. files; </p> <p> // 'status Information <br/> System. text. stringBuilder strMsg = new System. text. stringBuilder (); <br/> strMsg. append ("the uploaded files are: <br/> <pr color =" red "> "); <br/> try <br/> {<br/> for (int iFile = 0; iFile "); <br/> strMsg. append ("client file address:" + postedFile. fileName + "<br/>"); <br/> strMsg. append ("file name to be uploaded:" + f IleName + "<br/>"); <br/> strMsg. append ("File Upload Extension:" + fileExtension + "<br/> <pr/> "); <br/> // 'can be saved to different folders based on different extension names <br/> // Note: you may need to modify the anonymous write permission of your folder. <Br/> postedFile. saveAs (System. web. httpContext. current. request. mapPath ("images/") + fileName); <br/>}< br/> strStatus. text = strMsg. toString (); <br/> return true; <br/>}< br/> catch (System. exception Ex) <br/>{< br/> strStatus. text = Ex. message; <br/> return false; <br/>}< br/> # code generated by region Web forms designer <br/> override protected void OnInit (EventArgs e) <br/> {<br/> // COD EGEN: This call is required by the ASP. NET Web form designer. <Br/> // <br/> InitializeComponent (); <br/> base. onInit (e ); <br/>}</p> <p> // <br/> <summary> // The designer supports the required methods. Do not use the code editor to modify the method. <br/> /// content of this method. <Br/> // </summary> <p> private void InitializeComponent () <br/>{< br/> this. ID = "Upload"; <br/> this. load + = new System. eventHandler (this. page_Load); </p> <p >}< br/> # endregion <br/>}< br/> ........................................ ........................................ ........................................ ..........
Tip:
You 'd better make the upload an ifream so that even if you refresh it, it will not affect other people's data!