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