Image upload (ii)

Source: Internet
Author: User

This method requires the form form to be submitted and then jump to the page (no first method is good):

First, the JSP code:

<Divclass= "Tphoto"name= "Head_img"ID= "Head_img">    <inputtype= "File"name= "File"onchange= "Previewimage (this)" />    <DivID= "Preview">        <imgID= "Imghead"Border=0src= ' ${ctx}/static/images/elearning/photo_teacher.png '>    </Div></Div>


Second, JS code:

//image Upload    functionpreviewimage (file) {varMAXWIDTH = 100; varMaxHeight = 100; vardiv = document.getElementById (' preview '); if(File.files && File.files[0]) {div.innerhtml= '  '; varimg = document.getElementById (' Imghead '); Img.onload=function(){                    varRect =Clacimgzoomparam (MAXWIDTH, MaxHeight, Img.offsetwidth, img.offsetheight); Img.width=Rect.width; Img.height=Rect.height; Img.style.marginLeft= rect.left+ ' px '; Img.style.marginTop= rect.top+ ' px '; }              varReader =NewFileReader (); Reader.onload=function(EVT) {IMG.SRC =Evt.target.result;} Reader.readasdataurl (file.files[0]); } Else {            varSfilter= ' Filter:progid:DXImageTransform.Microsoft.AlphaImageLoader (sizingmethod=scale,src= "';            File.select (); varsrc =Document.selection.createRange (). text; Div.innerhtml= '  '; varimg = document.getElementById (' Imghead '); Img.filters.item (' DXImageTransform.Microsoft.AlphaImageLoader '). src =src; varRect =Clacimgzoomparam (MAXWIDTH, MaxHeight, Img.offsetwidth, img.offsetheight); Status= (' rect: ' +rect.top+ ', ' +rect.left+ ', ' +rect.width+ ', ' +rect.height); Div.innerhtml= "<div id=divhead style= ' width:" +rect.width+ "Px;height:" +rect.height+ "Px;margin-top:" +rect.top+ "PX; Margin-left: "+rect.left+" PX; " +sfilter+src+ "\" ></div> "; }    }        functionClacimgzoomparam (MaxWidth, maxheight, width, height) {varparam = {top:0, left:0, Width:width, height:height}; if(Width>maxwidth | | height>maxheight) {Ratewidth= width/MaxWidth; Rateheight= height/MaxHeight; if(Ratewidth >rateheight) {Param.width=MaxWidth; Param.height= Math.Round (Height/ratewidth); } Else{param.width= Math.Round (Width/rateheight); Param.height=MaxHeight; }} param.left= Math.Round ((maxwidth-param.width)/2); Param.top= Math.Round ((maxheight-param.height)/2); returnparam; }


Third, the background Java code:

/*** * @Title: Insert * @Description: NEW *@param @paramparams *@param @paramRequest *@param @return     * @returnMessageresp *@throws     */@RequestMapping ("/insert")     Public ModelandviewInsertmodel Model,Trainerqueryparam params, httpservletrequest request, Multipartfile file{ Modelandview view = new Modelandview (); User u=Sessionutils.getuser (Request.getsession ()); /*** The attributes to be updated in the parameter object passed by the front end are transferred to the corresponding field properties of the model entity class, which can be implemented by a generic conversion **/Trainer Trainer=NewTrainer (); Try{beanutils.copyproperties (trainer, params);//The front is the target, followed by the source object}Catch(illegalaccessexception e) {Throw NewAjaxbusinessexception ("New Error! "); } Catch(InvocationTargetException e) {Throw NewAjaxbusinessexception ("New Error! "); } trainer.setid (Uuidutil.getuuid ()); //IDtrainer.setcorpid (Sessionutils.getcorpid (Request.getsession ())); Try {                       if (!file.isempty ()) {//Get project under Upload resource bundle path, of course this package is already present in the String path = Request.gets                                Ession (). Getservletcontext (). Getrealpath ("Upload/trainer_picture");                The name of the uploaded picture is String fileName = File.getoriginalfilename (); String NewFileName = uuidutil.generateguid () + filename.substring (Filename.lastindexof (".")).                                toLowerCase ();                The following is the conversion of the upload path letter StringBuffer importpath = new StringBuffer ();                                String temp[] = Path.split ("\\\\");                    for (int i = 0; i < temp.length; i++) {importpath.append (temp[i]);                Importpath.append ("/");                } importpath.append (NewFileName);                To see if there is a upload package, no new upload package will be created as the path to the resource upload file TargetFile = new file (path, newfilename);          if (!targetfile.exists ()) {          Targetfile.mkdirs ();                                }//File Upload File.transferto (targetfile);            Trainer.setheadimg (NewFileName); }                        if(Trainerservice.insert (trainer) = = 1) {Resp.setresult ("True"); Resp.setresultdesc ("New success!" "); }        } Catch(Exception e) {Resp.setresult ("False"); Resp.setresultdesc ("New Error!" "); }//Model.addattribute ("Trainerresp", TRAINERRESP);
Model.addattribute ("Srlist", srlist);

View.setviewname ("Views/xxx/manage/xxxinfo");

return view; }

Image upload (ii)

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.