Ajax uploads images and previews

Source: Internet
Author: User

1. Directly on the simplest kind of Ajax upload images asynchronously and preview

Html:

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">5 <title>Image Upload | Cookies</title>6 </Head>7 <Body>8File<inputtype= "File"ID= "Images"name= "image" /><BR><BR>9Desc<inputtype= "text"ID= "desc"name= "desc" /><BR><BR>Ten     <inputtype= "button"value= "Upload"onclick= "upload ();"> One      A     <Divclass= "Images"></Div> -      - <Scripttype= "Text/javascript"src= "Js/jquery-1.12.4.min.js"></Script> the <Scripttype= "Text/javascript"src= "Js/upload.js"></Script> - <Scripttype= "Text/javascript"> -     functionupload () { - $.ajaxfileupload ({ + URL:'upload.htm', - Fileelementid:'Images', + DataType:'JSON', A data: {desc: $ ("#desc"). Val ()}, at Success:function(data) { -                 varHTML= $(". Images"). html (); - HTML+= ' "' +Data.url+ '">' -                 $(". Images"). HTML (HTML); -             } -         }) in         return false; -     } to </Script> + </Body> - </HTML>

Servlet:

1 protected voidDoPost (httpservletrequest request, httpservletresponse response)2             throwsservletexception, IOException {3Diskfileitemfactory factory =Newdiskfileitemfactory ();4         5Servletfileupload upload =Newservletfileupload (factory);6         7String path = Request.getservletcontext (). Getrealpath ("/upload");8String name =NULL;9         Try {Tenlist<fileitem> items =upload.parserequest (request); One              for(Fileitem item:items) { A                 if(Item.isformfield ()) { -System.out.println (Item.getfieldname () + ":" +item.getstring ()); -}Else { theName =item.getname (); -Item.write (NewFile (Path,name)); -                 } -             } +PrintWriter out =Response.getwriter (); -Out.print ("{"); +Out.print ("url:\" "+ name +" \ ""); AOut.print ("}"); at              -}Catch(Exception e) { - e.printstacktrace (); -         } -}

3. Here will use a ajaxupload.js, online much more, really can't find, also can private me, to you

Ajax uploads images and previews

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.