ASP (C #) implements code that dynamically uploads multiple images (multiple files) at once

Source: Internet
Author: User

When we do Web development for ASP, we often encounter the need to upload multiple files at once. Usually our solution is to put multiple upload file boxes fixed, so the solution is obviously unreasonable, because one upload multiple, it means that the quantity is uncertain. So we are going to let these file upload box dynamically add, below I do a gallery management of my upload image function as an example

The default is to upload a picture, but when we click the "Add Picture" button can be implemented to select multiple images and their description at the same time upload, this feature limit can only upload 8, and each picture size does not exceed 1M, this people can change according to the actual situation!

The first step is to use JavaScript code to dynamically add the File upload box and description text box, the key code is as follows:

Description: <input name= "text" type= "text" style= "width:150px" maxlength= "/>"
Step two: Server-side code implementation
Just upload a single file or picture, make the most common, but for such a one-time upload multiple files and their corresponding description of the problem, it will be a bit of trouble
First of all The System.Web.HttpContext.Current.Request.Files method obtains the client's file collection, then obtains the Description text box collection through the Request.Form method, finally uses a loop to upload the file, and saves the corresponding information to the database
The key code is as follows:

Finally, all the code for the above function is given:
HTML code:

 <%[email protected] Page language= "C #" codefile= "UploadImg.aspx.cs" inherits= "netadmin_apicture_uploadimg" %> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 

Background code: &NBSP;

 Using System; Using System.Data; Using System.Configuration; Using System.Collections; 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 NetAdmin_APicture_UploadImg:System.Web.UI.Page ... {protected void Page_Load (object sender, EventArgs e) ... {} protected void Btnupload_click (object sender, EventArgs e) ... {lblmessage.text = ""; Lblmessage.visible = false; System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; System.Text.StringBuilder strmsg = new System.Text.StringBuilder (""); string[] rd = request.form[1]. Split (', ');//Get the picture description of the text box string array, for the corresponding picture description string Albumid=ddlalbum.selectedvalue.trim (); int ifile; for (ifile = 0; ifile < files. Count; ifile++) ... {if (Files[ifile]. Filename.length > 0) ... {System.Web.HttpPostedFile postedfile = Files[ifile]; if (PostedFile. contentlength/1024 &GT 1024)//single file cannot be greater than 1024k ... {strmsg. Append (Path.getfilename (PostedFile. FileName) + "---cannot be greater than 1024k<br>"); Break } String fex = Path.getextension (PostedFile. FileName); if (Fex! = ". jpg" && fex! = ". JPG "&& fex! =". gif "&& fex! =". GIF ") ... {strmsg. Append (Path.getfilename (PostedFile. FileName) + "---picture is not formatted, only JPG or gif<br>"); Break }}} if (strmsg. Length <= 0)//Description picture size and format are no problem ... {//The following is the creation of the library directory string dirname = "Pic00" + DdlAlbum.SelectedValue.Trim (); string dirpath = Server.MapPath ("Http://www.cnblo Gs.com/picbase "); Dirpath = Dirpath + "\" + dirname; if (directory.exists (dirpath) = = False) ... {directory.createdirectory (dirpath);} Random ro = new random (); int name = 1; for (int i = 0; i < files. Count; i++) ... {System.Web.HttpPostedFile myFile = files[i]; string FileName = ""; String fileextention = ""; String picpath = ""; FileName = System.IO.Path.GetFileName (myfile.filename); String Stro=ro. Next (100,100000000). ToString () +name. ToString ();//Produce aRandom number for new named picture string NewName =datetime.now.minute.tostring () + DateTime.Now.Second.ToString () + DateTime.Now.Millisecond.ToString () +stro; if (Filename.length > 0)//have files to perform the upload operation and then save to the database ... {fileextention = System.IO.Path.GetExtension (myfile.filename); string ppath = Dirpath + "\" + NewName + fileextention; my File.saveas (Ppath); string fjname = FileName; Picpath = "Picbase" + "\" + dirname + "\" + NewName + fileextention; } addpicture (Picpath, Rd[i], albumid);//Save the picture information to the database if (name = = 1)//If the first picture is updated every time, it will be updated as the cover page of the album ... {upfirstimg (albumID, Picpath);} Name = name + 1;//The variable used to rename the rule}} else ... {Lblmessage.text = strmsg. ToString (); Lblmessage.visible = true; }} private void AddPicture (string imgpath,string imgnote,string albumid) ... {String sql = "Insert Wb_albumimges (Imgpath,imgnote,albumid) VALUES ('" +imgpath+ "', '" +imgnote+ "', '" +albumid+ "')"; DB MyDB = new db (); MyDB. Runproc (SQL); private void Upfirstimg (String albumid,string firstimg) ... {String sql = "Update wb_album set firstimg= '" +firstimg+ "' Where albumid=" +ALBUMID; DB MyDB = new db (); MyDB. Runproc (SQL);  } }

  

ASP (C #) implements code that dynamically uploads multiple images (multiple files) at once

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.