Struts multi-attachment upload

Source: Internet
Author: User
The dynamic file input field enables sturts to upload multiple files. Can you check whether the file can be optimized ??
This is the JSP page code:
JS is mainly used to generate dynamic file fields.
<SCRIPT>
VaR I = 0;
Function addelements (){
With (document. Forms ("Demo ")){
If (I> = 5 ){
Alert ("a maximum of five! ");
Return;
}
Insertadjacenthtml ("beforeend", "<input type =/" file/"id =" + I + "value =" + I + "name = input" + (I ++) + "> <br> ");
}
}
Function lastelement (){
With (document. Forms ("Demo ")){
Insertadjacenthtml ("beforeend", "<input type =/" hidden/"value =" + (I) + "name =/" maxnum/"> ");
}
}
</SCRIPT>
The following is the form for file upload:
<Form action = "$ {pagecontext. Request. contextpath}/multiupload. Do"
Name = demo method = "Post" enctype = "multipart/form-Data" onsubmit = "lastelement ();">
<Input type = "button" onclick = addelements (); value = "add">
<Input type = "Submit" name = "save" value = "save">
<Br>
</Form>

The above work can dynamically generate the file fields required.
See formbean:
Public class multiuploadform extends actionform {
Private formfile file;
// Multi-attachment upload should be an array
Private list <formfile> myfiles;
Public multiuploadform (){
Myfiles = new arraylist <formfile> ();
// To display a file on the page
Myfiles. Add (File );
}
Public list <formfile> getmyfiles (){
Return myfiles;
}
Public void setmyfiles (list <formfile> myfiles ){
This. myfiles = myfiles;
}

Public formfile GetFile (){
System. Out. println ("Run in uploadfile. GetFile ()");
Return file;
}
Public void setfile (formfile file ){
This. File = file;
}

}

Then there is action:
Public class multiuploadaction extends action {
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response) throws filenotfoundexception, ioexception {

Multiuploadform = (multiuploadform) form;
// Obtain all uploaded files:
Multipartrequesthandler mrh = multiuploadform. getmultipartrequesthandlEr ();
Map formfiles = mrh. getfileelements ();
// Copy uploaded files:
If (formfiles! = NULL ){
Iterator fileit = formfiles. Values (). iterator ();
While (fileit. hasnext ()){
Formfile forms = (formfile) fileit. Next ();
System. Out. println (forms. getfilename (); // indicates that the upload is successful}
}

Return NULL;
}
}
}

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.