In previous web applications, uploading files was a very troublesome task. Now, with. net, File Upload becomes 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. The Code is as follows:
Upload. aspx
<% @ 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>
Upload. aspx. CS
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Namespace webportal
{
/// <Summary>
/// Summary of upload.
/// Multifile upload
/// </Summary>
Public class upload: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Button uploadbutton;
Protected system. Web. UI. webcontrols. Label strstatus;
Private void page_load (Object sender, system. eventargs E)
{
/// Place user code here to initialize the page
If (this. ispostback) This. saveimages ();
}
Private Boolean saveimages ()
{
/// 'Traverse the file form Element
Httpfilecollection files = httpcontext. Current. Request. files;
/// 'Status Information
System. Text. stringbuilder strmsg = new system. Text. stringbuilder ();
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> ");
/// 'Can be saved to different folders based on different extension names
/// Note: you may need to modify the anonymous write permission of your folder.
Postedfile. saveas (system. Web. httpcontext. Current. Request. mappath ("images/") + filename );
}
}
Strstatus. Text = strmsg. tostring ();
Return true;
}
Catch (system. Exception ex)
{
Strstatus. Text = ex. message;
Return false;
}
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. ID = "Upload ";
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}