ASP. NET 2.0 multi-File Upload method [Abstract]
The Multifile upload method is actually very simple. You can add multiple HTML file upload controls on the page Code Use the following statement system. web. httpcontext. current. request. files.
[Full text]
The following code example uploads multiple files to the server, displays them in tables and lists, and deletes the files selected in the list box from the server.
[Page file HTML]
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> Multifile upload example </title>
<Style type = "text/CSS" Media = screen>
<! --
. Fileupstyle {
Font-size: 12px;
Font-weight: lighter;
Font-variant: normal;
Border: 1px solid #006699;
Width: 250px;
}
-->
</Style>
</Head>
<Body style = "font-size: 9pt; color: #000066;">
<Form ID = "form1" runat = "server" enctype = "multipart/form-Data">
Multifile upload example <br/>
<Input type = "button" value = "added" onclick = "AddFile ()" style = "border-Right: # 6699cc 1px solid; border-top: # 6699cc 1px solid; font-size: 9pt; border-left: # 6699cc 1px solid; width: 60px; border-bottom: # 6699cc 1px solid ">
<Input type = "button" value = "reset" style = "border-Right: # 6699cc 1px solid; border-top: # 6699cc 1px solid; font-size: 9pt; border-left: # 6699cc 1px solid; width: 60px; border-bottom: # 6699cc 1px solid; height: 20px "id =" button1 "Language =" JavaScript "onclick =" Return button#onclick () ">
<Asp: button runat = "server" text = "Upload" id = "Upload" onclick = "upload_click1" bordercolor = "desktop" borderwidth = "1px" Height = "20px" width = "60px "> </ASP: button>
<Asp: button id = "btn_refresh" runat = "server" bordercolor = "desktop" borderwidth = "1px" Height = "20px"
TEXT = "Refresh directory" width = "60px" onclick = "btn_refresh_click"/> <br/>
<Div id = "myfile">
<Input name = "file" type = "file" class = "fileupstyle" id = "file1"/>
<Br/>
</Div>
Uploaded file: <asp: linkbutton id = "btn_del" runat = "server" onclick = "btn_del_click" onclientclick = "Return confirm ('Are you sure you want to delete this file on the website? ') "> Delete the selected file </ASP: linkbutton> <br/>
<Asp: dropdownlist id = "drp1" runat = "server" width = "250px">
</ASP: dropdownlist>
& Nbsp;
<Asp: Table id = "tabledirinfo" runat = "server" forecolor = "#000040">
</ASP: Table>
</Form>
</Body>
</Html>
[ Program Code File Code]
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void upload_click1 (Object sender, eventargs E)
{
Httpfilecollection _ FILES = system. Web. httpcontext. Current. Request. files;
For (INT I = 0; I <_ FILES. Count; I ++)
{
String names filename = _ FILES [I]. filename. substring (_ FILES [I]. filename. lastindexof ("\") + 1 );
If (Response filename! = "")
{
_ FILES [I]. saveas (server. mappath ("~ /Files/"+ program filename ));
// Page. response. Write ("File Name: [" + response filename + "] uploaded successfully! <Br> ");
}
}
}
Private void showfilelist ()
{
String strcurdir, filename, fileext;
// File Size
Long filesize;
// Last modification time;
Datetime filemodify;
// At initialization, the default value is the directory of the current page
Strcurdir = server. mappath ("Files ");
Fileinfo fi;
Directoryinfo dir;
Tablecell TD;
Tablerow TR;
Tr = new tablerow ();
// Dynamically add cell content
TD = new tablecell ();
TD. Controls. Add (New literalcontrol ("file name "));
Tr. cells. Add (TD );
TD = new tablecell ();
TD. Controls. Add (New literalcontrol ("file type "));
Tr. cells. Add (TD );
TD = new tablecell ();
TD. Controls. Add (New literalcontrol ("file size "));
Tr. cells. Add (TD );
TD = new tablecell ();
TD. Controls. Add (New literalcontrol ("Last modified "));
Tr. cells. Add (TD );
Tabledirinfo. Rows. Add (TR );
// Create a directory reference object for the current directory
Directoryinfo dirinfo = new directoryinfo (strcurdir );
// Clear all rows in the table
For (Int J = 0; j <tabledirinfo. Rows. Count; j ++)
Tabledirinfo. Rows. removeat (j );
Drp1.items. Clear ();
// Cyclically determine the files and directories under the current directory
Foreach (filesysteminfo FSI in dirinfo. getfilesysteminfos ())
{
Filename = "";
Fileext = "";
Filesize = 0;
// If it is a file
If (FSI is fileinfo)
{
FI = (fileinfo) FSI;
// Get the file name
Filename = Fi. Name;
Drp1.items. Add (filename );
// Get the file extension
Fileext = Fi. extension;
// Obtain the file size
Filesize = Fi. length;
// Get the last modification time of the file
Filemodify = Fi. lastwritetime;
}
Else // otherwise it is a directory
{
Dir = (directoryinfo) FSI;
// Obtain the directory name
Filename = dir. Name;
// Obtain the last modification time of the Directory
Filemodify = dir. lastwritetime;
// Set the file extension to "folder"
Fileext = "folder ";
}
// Dynamically Add Table content
Tr = new tablerow ();
TD = new tablecell ();
TD. Controls. Add (New literalcontrol (filename ));
Tr. cells. Add (TD );
TD = new tablecell ();
TD. Controls. Add (New literalcontrol (fileext ));
Tr. cells. Add (TD );
TD = new tablecell ();
TD. Controls. Add (New literalcontrol (filesize. tostring () + "Byte "));
Tr. cells. Add (TD );
TD = new tablecell ();
TD. Controls. Add (New literalcontrol (filemodify. tostring ("yyyy-mm-dd hh: mm: SS ")));
Tr. cells. Add (TD );
Tabledirinfo. Rows. Add (TR );
}
}
Protected void btn_refresh_click (Object sender, eventargs E)
{
// Refresh the Directory List
Showfilelist ();
}
Protected void btn_del_click (Object sender, eventargs E)
{
// Delete the selected file
If (drp1.items. Count! = 0)
{
System. Io. file. Delete (server. mappath ("files/" + drp1.selecteditem. Text ));
Showfilelist ();
}
}
}
Method 2
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
}
}