ExtJS: File Upload Instance
var Ext_dateformat = ' y-m-d h:i:s '; var dateformat = ' Yyyy-mm-dd HH:mm:ss '; var date = new Date (); Ext.onready (function () {var fifp =ext.create (' Ext.form.Panel ', {renderto: ' Fi-form ', width:500, FRA Me:true, Title: ' File Upload ', bodypadding: ' 0 ', defaults: {anchor: ' 100% ', AL Lowblank:false, Msgtarget: ' Side ', labelwidth:80}, items: [{xtype: ' Tex Tfield ', Fieldlabel: ' Sample number ', ID: ' Finfo ', Name: ' Finfo '},{xtype: ' Containe R ', layout: ' Hbox ', items: [{xtype: ' TextField ', Fieldlabel: ' Current longitude ', ID: ' Flongitude ', Name: ' Flongitude ', Msgtarget: ' Side ', all Owblank:false, labelwidth:80}, {xtype: ' TextField ', Fieldlabel : ' Current latitude ', id: ' Flatitude ', Name: ' Flatitude ', Msgtarget: ' side ', Allowblank:false, labelwidth :},{xtype}]: ' TextField ', Fieldlabel: ' Upload time ', id: ' Ftime ', Name: ' Ftime ',//YYYY-MM-DD HH:mm:ssva Lue:Ext.Date.format (New Date (Date.getfullyear (), Date.getmonth (), Date.getdate (), date.gethours (), Date.getminutes ( ), Date.getseconds ()), Ext_dateformat), listeners: {' Focus ': function () {Wdatepicker ({datefmt:dateformat});}} },{xtype: ' Filefield ', id: ' Fiupload ', Emptytext: ' Please click on the right button to select the file! ', Fieldlabel: ' Select File ', Name: ' Fiupload ', ButtonText: ' Browse for file ', Buttonconfig: { Iconcls: ' Upload-icon '}], buttons: [{text: ' Save file ', handle R:function () {var fiform = this.up (' form '). GetForm (); if (Fiform.isvalid ()) {Fiform.submit ({type: ' Ajax '), URL: ' Files/adddata.action ', Method: "POST", waitmsg: ' Positive On upload, please wait ... ', success:function (form, action) {Ext.Msg.alert (' success ', ' File upload Success! ‘); }, Failure:function (form, action) {Ext.Msg.alert ("failure", "File upload Failure "); } }); }}},{text: ' re-upload ', handler:function () {this.up (' form '). GetForm (). reset (); } }] });});
Background processing Core class methods:
private static final int buffer_size = 1024;public String addData () throws Exception {Timestamp ts = new Timestamp (Sy Stem.currenttimemillis ()); SimpleDateFormat df = new SimpleDateFormat ("yyyymmddhhmmsssss");//Set date format ts = timestamp.valueof (this.ftime); SYSTEM.OUT.PRINTLN (TS); String nowtime = Df.format (New Date ()); System.out.println ("uploadfilename =" + This.fiuploadfilename); System.out.println ("Uploadcontenttype =" + This.fiuploadcontenttype); System.out.println (nowtime);//upload--Wapps The following folder, used to hold the picture string tosrc = Servletactioncontext.getservletcontext (). Getrealpath ("upload") + "/" + Nowtime + getfileexp (this.fiuploadfilename); Use time stamp as file name string tofilename = Nowtime + getfileexp (this.fiuploadfilename); String Tosrcpath = "./upload/" + tofilename; String Toinfo = this.finfo;double tolongitude = double.parsedouble (this.flongitude);D ouble tolatitude = Double.parsedouble (This.flatitude); SYSTEM.OUT.PRINTLN ("Original filename:" + this.fiuploadfilename); SYSTEM.OUT.PRINTLN ("File Description:" + toinfo); SysteM.OUT.PRINTLN ("Storage path:" + Tosrcpath); System.out.println ("Store file name:" + tofilename); System.out.println ("Current Longitude:" + tolongitude); System.out.println ("Current dimension:" + Tolatitude); File ToFile = new file (TOSRC); WriteFile (This.fiupload, tofile); Files Files = new files (ts, tofilename, Tosrcpath, Toinfo, tolatitude,tolongitude); String result = Filesservice.adddata (files); SYSTEM.OUT.PRINTLN (result); success = True;return success;} private static void WriteFile (file src, file dst) {System.out.println ("= = File Write = ="); try {inputstream in = Null;outputstr EAM out = null;try {in = new Bufferedinputstream (new FileInputStream (SRC), buffer_size); out = new Bufferedoutputstream (new FileOutputStream (DST), buffer_size); byte[] BUFFER = new Byte[buffer_size];while (In.read (BUFFER) > 0) {out.write ( buffer);}} finally {if (null! = in) {In.close ();} if (null! = out) {out.close ();}}} catch (Exception e) {e.printstacktrace ();} System.out.println ("= = Write succeeded! == ");}
ExtJS: File Upload Instance