Java image upload and preview, front desk with jquery plugin ajaxfileupload, backstage with Fileutils.copyfile.

Source: Internet
Author: User
Tags jquery library

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.

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.