Personal notes for later use.
Table Body Code:
<TD colspan= "3" ><s:file label= "upload" name= "UploadFile" id= "Uploadnumfile" onchange= "FileUpload (' Uploadnumfile ', ' goodsrecorddtl_goodspicture ', ' goodspicture_img ') "/><a href=" # "title=" View "onclick=" ViewPic ( ' ViewPicture1 ', ' goodspicture ', ' goodsrecorddtl_goodspicture '); > View pictures </a><div id= "ViewPicture1" title= "Picture preview" style= "Display:none;" align= "center" ></div></td>
JS code (remember to introduce the jquery library and the Ajaxfileupload Library):
Upload file ID number function FileUpload (uploadfileid,filepathid,imgid) {var imp= document.getelementbyid (Uploadfileid); if (Imp==null | | imp== "" | | imp== undefined) {alert (' Please select file '); Return } $.ajaxfileupload ({url:ct + "uploadfile.do", Secureuri:false, Fileelementid:uploadfileid, DataType: ' Multipa Rt/form-data ', success:function (data, status) {var json = eval (' (' + data + ') '); if (json.msg== "1") {alert ("Upload succeeded"); $ (' # ' +filepathid). Val (Json.path); $ (' # ' +imgid). attr ("src", etrade.ctx+ "/upload/" +json.path); }else{alert ("File upload failed"); }}, Error:function (data, status, E) {alert (e); } }); } function Viewpic (Dialogid,imgid,fileid) {$ ("#" +dialogid). Dialog ({height:350, width:600, buttons: {"Cancel ": Function () {$ (" # "+imgid). attr (" src "," "); $ (This). dialog ("Close"); }}, Close:function () {$ ("#" +imgid). attr ("src", ""); } }); if ($ ("#" +fileid). val () = = "") {return; } $ ("#" +imgid). attr ("sRC ", ct+"/toview.do?attachment= "+$ (" # "+fileid). Val ()); }
Background code (UploadFile naming must match the foreground name= "UploadFile" value):
Private File UploadFile; Public File Getuploadfile () {return uploadfile; } public void Setuploadfile (File uploadfile) {this.uploadfile = UploadFile; } @Action (value = "UploadFile", results = {@Result (name = "Success", type = "json", params = {"Ignorehiera Rchy "," false "," ContentType "," text/html "," root "," DataMap "})}) public String UploadFile () {try { String upload_path= Servletactioncontext.getservletcontext (). Getrealpath ("/upload/"); String randomname= Uuid.randomuuid (). toString () + ". jpg"; if (uploadfile!=null) {file StorageFile = new File (Upload_path + "/" +randomname); Fileutils.copyfile (UploadFile, storageFile); } datamap.put ("MSG", 1); Datamap.put ("path", randomname);//file ID number} catch (Exception e) {logger.error ("", e); Datamap.put ("MSG", 0); } return SUCCESS; }
The above code may be a little incomplete, but the general idea is this, just for later use when convenient to view.
Java image upload and preview, front desk with jquery plugin ajaxfileupload, backstage with Fileutils.copyfile.