HTML5 Formdata upload for app side

Source: Internet
Author: User


function
UploadFile () {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

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.