Today, boss asked for a bulk file upload function. After half a day of work, I finally got it done. I hope our predecessors will give more instructions. Let's take a look at it. (here is the simplified version, which only introduces the main implementation process, no beautification. Do not blame! Do not blame !) :
Click Add file to automatically add the fileupload control.
Click Browse to select multiple files to upload
Click Upload File to complete file upload.
Let's not talk about it. The following is the code:
Foreground default. aspx code
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %> <! 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">
Default. aspx. CS Code (which of the following annotations can be understood ?)
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. collections; // introduce the namespace public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {If (! Page. ispostback) // the first execution page {sfupc () ;}# region this method is used to save the current page Upload File control set to private void sfupc () in the session () {arraylist Al = new arraylist (); // dynamically Add the array foreach (control C in tab_updownfile.controls) {// locate the fileupload control in the table and add it to the arraylist if (C. getType (). tostring () = "system. web. UI. htmlcontrols. htmltablerow ") {htmltablecell HTC = (htmltablecell) C. controls [0]; foreach (control FUC in HTC. controls) {If (FUC. gett Ype (). tostring () = "system. web. UI. webcontrols. fileupload ") {fileupload Fu = (fileupload) FUC; // sets the fileupload style Fu. bordercolor = system. drawing. color. dimgray; Fu. borderwidth = 1; // Add the fileupload control Al. add (Fu) ;}}}// add arraylist to session. add ("filescontrols", Al) ;}# endregion # region this method is used to add a file upload control private void insertc () {// instantiate arraylist Al = new arraylist (); this. tab_updown File. rows. clear (); // clear all rows in table F whose ID is getinfo (); // indicates the <tr> HTML element htmltablerow HTR = new htmltablerow () in the htmltable control (); // The <TD> and <TH> HTML elements in the htmltablerow object htmltablecell HTC = new htmltablecell (); // Add a fileupload control HTC to the cell. controls. add (New fileupload (); // Add the cell hTR in the row. controls. add (HtC); // Add the table row tab_updownfile.rows.add (HTR); sfupc ();} # endregion # region this method is used to add the Upload File control set saved in the session to the table PRI Vate void getinfo () {arraylist Al = new arraylist (); If (session ["filescontrols"]! = NULL) {Al = (arraylist) session ["filescontrols"]; for (INT I = 0; I <Al. count; I ++) {htmltablerow HTR = new htmltablerow (); htmltablecell HTC = new htmltablecell (); HTC. controls. add (system. web. UI. webcontrols. fileupload) al [I]); HTR. controls. add (HtC); tab_updownfile.rows.add (HTR) ;}}# endregion # region this method is used to perform the file upload operation private void upfile () {// obtain the folder path string filepath = server. mappath (". /") +" F Ile "; // get the collection of files uploaded by the client httpfilecollection HFCs = request. files; For (INT I = 0; I
Complete!
Please modify the settings as needed!