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;
}
}
}