Implements no jump to send form data, files, and can receive data returned in the background. Main technical points: 1, Form formAdd the target property, specify a Iframme name;form form submitted after the IFRAME in the embedded window to accept the response, the main page will no longer jump . 2. At the same time, we need to receive the data returned in the background for our business processing, here let the background return a script with the execution of a function tag .
<body><center> <div> <form id="AddForm" class="form"Method="Post"Enctype="Multipart/form-data" target= "Xmlfile_iframe"style="padding:5% 0% 5% 12%;"> <table id="itemTable" class="form"> <tr> <TDclass="Formtitle"><s:text name="Cems.dataimport"></s:text><font face="Song Body">*</font></td> <TDclass="Formvalue"><input type="text"Name="txt" ReadOnly class="Form-control"/></td> <td><input type="Button"Value='<s:text name= "Cems.choosefile" ></s:text>'Size=" -"onclick="Upload_xmlfile.click ()"style="width:73px;" class="btn Btn-primary"/> <input type="file"Id="Upload_xmlfile"Onchange="Txt.value=this.value"Name="xmlfile"style="Position:absolute;filter:alpha (opacity=0);-moz-opacity:0;opacity:0;margin-left:-286px; height:25px;"Size=" -"/> </td> </tr> </table> </form> </div> <divclass="Windowbuttondiv"style="padding-top:30px;"> <buttonclass="btn Btn-primary"Id="Submitbutton"><span><iclass="FA Fa-check"></i></span><span><s:text name="Cems.ok"></s:text></span></button> <buttonclass="btn Btn-primary"Id="CloseWindow"onclick="dialogclose ()"><span><iclass="FA Fa-close"></i></span> <span><s:text name="Cems.cancel"></s:text></span></button> </div> <divclass="Alert Alert-warning"style="text-align:center;margin-top:20px"><iclass="FA Fa-info"> Note: Please select an XML file </i></div></center><iframe name= "Xmlfile_iframe" style= "Display:none" ></iframe></body>
<script type="Text/javascript">$ (function () {$ ("#submitButton"). Bind ("Click", function () {if($("#addForm"). ValidForm ()) {varIndex = $ ("#upload_xmlfile"). Val (). LastIndexOf (".")+1; varExt = $ ("#upload_xmlfile"). Val (). substr (Index). toUpperCase ();//get the file suffix name//file format filtering, supported only in XML format if(ext=='XML'){ $("#addForm"). attr ("Action","${basepath}/soft/kvssoftaction_importdo.do"); $("#addForm"). Submit (); $( This). attr ("Disabled","Disabled"); Loading (true); }Else{dialogmsg ('<s:text name= "Cems.soft.xmlfileTypeError" ></s:text>',0); } } });}); function Setmessage (data) {Loading (false); if(data = = ="Success") {dialogmsg ("Import Successful! ",1); Dialogclose (); }Else{dialogmsg ("Import Failed",-1); }}</script>
/** * Data import*/ Public voidImportdo () {Log.info ("Data import-st"); This. msg=Result_fail; Kvssoft Tempkvssoft=NULL; //list<string> failedfailname = new arraylist<string> ();String Subfilename =""; Try{normaluploadfileparser Normaluploadfileparser=NewNormaluploadfileparser (); List<FileInfoHolder> fileinfoholder=normaluploadfileparser.parserequest (Getrequest ()); InputStream InputStream=fileinfoholder.Get(0). getInputStream (); String Nameall= Fileinfoholder.Get(0). GetFileName (); Subfilename= Nameall.substring (Nameall.lastindexof ("\\")+1, Nameall.length ()); Kvssoftparse KVS=NewKvssoftparse (); List<KvsSoft> list =Kvs.xmltoobject (InputStream); for(Kvssoft kvssoft:list) {//Query from database First, if there is update, no then insertTempkvssoft =Kvssoftservice.querybyid (Kvssoft.getid ()); if(tempkvssoft==NULL){ //Failedfailname.add (Tempkvssoft.getname ());Kvssoftservice.save (Kvssoft); }Else{tempkvssoft.setname (Kvssoft.getname ()); Tempkvssoft.settype (Kvssoft.gettype ()); Tempkvssoft.setcompany (Kvssoft.getcompany ()); Tempkvssoft.setflag (Kvssoft.getflag ()); Tempkvssoft.setdesinfo (Kvssoft.getdesinfo ()); Tempkvssoft.seturl (Kvssoft.geturl ()); Tempkvssoft.setpublishtime (Kvssoft.getpublishtime ()); Tempkvssoft.setversion (Kvssoft.getversion ()); Tempkvssoft.setinstallpath (Kvssoft.getinstallpath ()); Tempkvssoft.setenginever (Kvssoft.getenginever ()); Tempkvssoft.setvirusbasever (Kvssoft.getvirusbasever ()); Tempkvssoft.setsoftversion (Kvssoft.getsoftversion ()); Tempkvssoft.setsoftenginever (Kvssoft.getsoftenginever ()); Tempkvssoft.setsoftvirusbasever (Kvssoft.getsoftvirusbasever ()); //Failedfailname.add (Tempkvssoft.getname ());kvssoftservice.update (Tempkvssoft); } } This. msg=result_success; Log.info ("Data Import Successful! "); Inputstream.close (); } Catch(Exception e) {log.error ("Bulk Import Error", E); }finally{Manageroperaterlogservice.save ("Cems_operatekvssoftimport",Newstring[]{subfilename}); StringBuilder SB =new StringBuilder (); Sb.append ("<script type= ' Text/javascript ' >"); Sb.append ("Window.parent.setMessage ('" +this.msg+ ")"); Sb.append ("</script>"); Print (sb.tostring ()); Log.info ("record operations to log table-end-return data"+sb.tostring ()); }}
Before submitting the upload file: <script type= "Text/javascript" >window.parent.setmessage (' success ') </script>
Where Window.parent represents the parent of the hidden IFrame, the Setmessage function is called in the parent. Of course, this setmessage function has to be defined in the parent area, otherwise it will be reported:Window.parent.setMessage is not a function error.
The main background is to send a data such as: Res.send (' <script>window.parent.uploadsuccess (' +data+ ') </script> ');
StringBuilder SB =new StringBuilder () sb.append ("<script type= ' Text/javascript ' >"); Sb.append (" Window.parent.setMessage (' +this.msg+ '), Sb.append ("</script>");p rint (sb.tostring ());
There is also a way to obtain data from the background by using the Ajaxform method of Jquery-form .
Refer to the previous blog post: Upload files from the form form to get data from the background
From form implementation No jump upload file, receive page background data