The use of user controls can be greatly improved Program Development progress, at the same time, also make Code The specifications are neat and the program structure is clear. This article implements a multi-File Upload user control based on the actual project. The following describes the functions of the control: 1. You can upload multiple files, control the number of uploads, and add file descriptions; 2. You can control the format type of the uploaded file. By setting different formats, you can upload multiple images, upload multiple files, and upload multiple videos. Of course, you can also perform hybrid upload; 3. the uploaded file is displayed in the list. You can view the image and delete the uploaded image. The control looks as follows: Widget appearance Because the project is based on the layered mode, the control implementation also defines a dataset -- imagedata at the common layer. The following is part of its code: Public class imagesdata: datatable { // You can add other file information. The file is saved in the format of URL + Info; Url + Info ;... Public const string url_field = "url_field "; Public const string info_field = "info_field"; private imagesdata (serializationinfo info, streamingcontext context) : Base (Info, context) { } Public imagesdata () { This. Columns. Add (New datacolumn (url_field, typeof (system. String ))); This. Columns. Add (New datacolumn (info_field, typeof (system. String ))); } Public void fillimagedataset (string images, imagesdata imagedata) { If (images = NULL | images. Trim (). Length = 0) { Return; } String [] items = images. Split (New char [1] {';'}, stringsplitoptions. removeemptyentries ); String [] url_info = NULL; Foreach (string item in items) { Datarow DR = imagedata. newrow (); Url_info = item. Split (':'); Dr [url_field] = url_info [0]; Dr [info_field] = url_info [1]; Imagedata. Rows. Add (DR ); } } Public void appendimagechunk (stringbuilder Sb, string URL, string info) { If (sb. length! = 0) { SB. append (";"); } SB. append (URL). append (":"). append (Info ); } } } The following describes how to implement the wucmultifileupload control: Public const string images_table = "images_table "; Private stringbuilder images = new stringbuilder (); // path property value: URL + info Bool enabled = true; Private string attachmentdir = ""; // specifies the upload path. Private int number = 0; // Number of allowed uploads Private string format = ""; // The type of the file format that can be uploaded; null is not limited Implementation principle: Determine the type of the file to be uploaded before uploading. If the control allows you to upload files in this format when setting properties, the file will be uploaded and counted at the same time, you can upload files only when the file type is allowed and the number of uploads does not exceed the limit. Otherwise, an error message is displayed. For each uploaded image, use viewstate [images_table] = imagedata to keep the uploaded image and display it in datalist. Datalist displays the description of the file and a delete button. The description is a hyperlink that connects to a file. The delete button deletes the file by binding the File URL value. The key code is as follows: Private void bindimages (string imagevalues) { If (imagevalues. Equals (string. Empty )) Return; imagesdata imagedata = new imagesdata (); Imagedata. fillimagedataset (imagevalues, imagedata); dlattachments. datasource = imagedata; Dlattachments. databind (); } Protected void btupload_click (Object sender, eventargs E) { Int num = 0; // Number of uploaded files If (viewstate ["num"]! = NULL) Num = (INT) viewstate ["num"]; // implements the upload. At the same time, the path is spelled out and saved in images. If (fuupload. filename = NULL | fuupload. filename. Length = 0 | fuupload. filecontent = NULL) { Return; } If (number! = 0) // number = 0 no quantity limit { If (viewstate ["num"]! = NULL & (INT) viewstate ["num"]> = number) { Lbmessage. Text = "the maximum number of uploads has been reached! "; Return; } } If (validformat (getextendname (fuupload. filename ))) { Imagesdata imagestable = (imagesdata) viewstate [images_table]; If (imagestable! = NULL & imagestable. Rows. Count> 0) { Foreach (datarow DR in imagestable. Rows) { Images. append (Dr [imagesdata. url_field]. tostring ()); Images. append (":"); Images. append (Dr [imagesdata. info_field]. tostring ()); Images. append (";"); } } // Upload Random random = new random (); String filename = getfittimestring () + random. Next (). tostring () + "." + getextendname (fuupload. filename ); String url = attachmentdir + "\" + filename; String info = tbinfo. Text. Trim (); Try { Fuupload. saveas (server. mappath (URL )); Num ++; Viewstate ["num"] = num; } Catch { } // The value stored to DB Images. append (URL ); Images. append (":"); Images. append (Info ); Images. append (";"); Viewstate ["Images"] = images; Images = images. tostring (); tbinfo. Text = ""; Lbmessage. Text = ""; } } Protected void lbdelattachment_click (Object sender, eventargs E) { Try { Linkbutton lB = (linkbutton) sender; String url = LB. commandargument. Trim (); Stringbuilder sb = new stringbuilder (); Imagesdata imagetable = (imagesdata) viewstate [images_table]; Foreach (datarow DR in imagetable. Rows) { String rowurl = Dr [imagesdata. url_field]. tostring (); String rowinfo = Dr [imagesdata. info_field]. tostring (); If (! Rowurl. Equals (URL )) { SB. append (rowurl). append (":"). append (rowinfo). append (";"); } Else { File. Delete (server. mappath (URL )); } } Viewstate ["Images"] = Sb; Images = sb. tostring (); } Catch (exception ex) { Lbmessage. Text = ex. message; } } |