Javascript dynamic generation of graphic upload control implementation code

Source: Internet
Author: User

When uploading files, you sometimes need to upload multiple images. here we need to dynamically generate the control for uploading images based on the user's actual situation. js is fully available here.

STEP/Method

① Change the Form label in the aspx page

The Code is as follows: Copy code
: <Form id = "form1" runat = "server" enctype = "multipart/form-data">,

That is to say, the original added enctype = "multipart/form-data"
② Add the following code to the page, which contains a div container to facilitate the dynamic addition of controls to the Container Using js

The Code is as follows: Copy code
<Div align = "left" id = "div_Pic" style = "border: 1px solid # CCCCCC">
<Input name = "PicFile" type = "file" id = "ShowPicFile" onClick = "createInput ('div _ Pic ', 'picfile')">
</Div>

③ Add js Code:

The Code is as follows: Copy code

<Script language = "javascript" type = "text/ecmascript">
Function createInput (parentID, inputFileID ){
Var parent = $ (parentID); // obtain the parent Element
Var div = document. createElement ("div"); // create a div container to include the input file
Var x = parseInt (Math. random () * (80-1) + 1;
Var divName = inputFileID + x. toString (); // name of the random div container
Div. name = divName;
Div. id = divName;
Var aElement = document. createElement ("input"); // create input
AElement. name = inputFileID;
AElement. id = inputFileID;
AElement. type = "file"; // set the type to file.
AElement. onclick = function () {createInput ("div_Pic", "PicFile ")};
Var delBtn = document. createElement ("input"); // create a Button to delete the input file.
DelBtn. type = "button ";
DelBtn. value = "delete ";
DelBtn. onclick = function () {removeInput (parentID, divName)}; // set the onclick method for the button
Div. appendChild (aElement); // Add the input file to the div container
Div. appendChild (delBtn); // Add the delete button to the div container
Parent. appendChild (div); // Add the div container to the parent Element
}
Function removeInput (parentID, DelDivID ){
Var parent = $ (parentID );
Parent. removeChild ($ (DelDivID ));
}
// Obtain the elements in the document using the element ID
Function $ (v) {return document. getElementById (v );}
</Script>

④ Effect display:

 

When you click browse, a new upload control is added. If you do not need it, click Delete next to it.
⑤ Server-side processing code:

The Code is as follows: Copy code
System. Web. HttpFileCollection files = System. Web. HttpContext. Current. Request. Files;
For (int I = 0; I <files. Count; I ++)
{
System. Web. HttpPostedFile filePicture = files [I];
String FileType; // File Upload type (Extension)
FileType = System. IO. Path. GetExtension (filePicture. FileName). ToLower ();
String sFileName = Guid. NewGuid (). ToString () + FileType;
FilePicture. SaveAs (Server. MapPath ("HotPic \" + sFileName); // Save the image
}

You can use System. Web. HttpContext. Current. Request. Files; to obtain a set of all uploaded Files, and then traverse and upload.

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.