functionUploadFile () {if((document.getElementById ("file"). files[0].size/1024). toFixed (2) > 2048) {alert (' Photos cannot be greater than 2m! ‘); }Else{ varOldpic = $ ("#picurl"). Val (); varurl = ""; if(Oldpic! = "") {URL= "weixin/uploadtx?oldpic=" +Oldpic; }Else{URL= "Weixin/uploadtx?oldpic="; } $('. Photo_loading '). Show (); varFD =NewFormData (); Fd.append ("Header_img_id", document.getElementById ("file"). files[0]); varXHR =NewXMLHttpRequest (); //What to do after the request is completedXhr.onload=function(evt) {varFILEURL =Evt.target.responseText; FILEURL=Delhtmltag (FILEURL); varHtfileurl = Fileurl.replace ("Thumb_", "" "); $("#picurl"). Val (Htfileurl); $("#tx-img"). attr ("src", "upload/" +FILEURL); $("#tx-img"). Show (); $("#photo_add"). Hide (); $('. Photo_loading '). Hide (); } xhr.open ("POST", URL); Xhr.send (FD); } }
Css
. Upload_image{width:70px;Height:70px;position:Absolute;Top:2px; Left:2px;background:#EEE;Opacity:0;cursor:Pointer;}. Photo{position:relative;width:70px;Height:70px;background-size:100%;}. photo_loading{position:Absolute;Top:2px; Left:2px;Z-index: About;background:URL (.. /.. /images/photo_loading.gif) no-repeat 0 0;Opacity:0.8;Filter:Alpha (opacity=80);width:70px;Height:70px;Display:None;}. Photo_add{width:70px;Height:70px;text-align:Center;Line-height:70px;Border:2px dotted #ccc;font-size:24px;}
Html
<Divstyle= "margin-top:10px"> <labelclass= "Div-label"><spanclass= "X">*</span>I Photo:</label> <Divclass= "Photo"> <Divclass= "Photo_loading"></Div> <Divclass= "Photo_add"ID= "Photo_add">+</Div> <imgsrc=""style= "width:70px;height:70px;display:none;border:2px dotted #ccc;"ID= "Tx-img"/> <inputID= "File"name= "File"type= "File"class= "Upload_image"onchange= "UploadFile ()"> </Div> <Pstyle= "Margin-top:10px;font-size:13px;color: #999">(Size less than 2M)</P> </Div>
Control layer
@RequestMapping (value= "/UPLOADTX") @ResponseBody Publicstring Uploadtx (Multipartfile header_img_id, String oldpic) {//Delete if(!oldpic.equals ("") ) {String Realpath=Filerootpathserv.getrealpath (oldpic); File File=NewFile (Realpath); if(File.exists ()) {file.delete (); }} String FileUrl= ""; //Upload Image if(!Header_img_id.isempty ()) {Filemodel Filemodel= Fileuploadsvc.uploadimg (header_img_id, "TX"); FILEURL=Filemodel.getfileurl (); } returnFileUrl; }
HTML5 Formdata upload for app side