Two ways of achieving
index.jsp file
<%@page ContentType="text/html"pageencoding="UTF-8"%><!DOCTYPE HTML><HTML> <Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"> <title>JSP Page</title> <Linkrel= "stylesheet"type= "Text/css"href= "Css/common.css" /> <Scripttype= "Text/javascript"src= "Js/jquery-1.11.1.js" ></Script> <Scripttype= "Text/javascript"> //way to realize 1:url way functionpreview1 (file) {varimg= NewImage (), URL=img.src=url.createobjecturl (file); var$img=$ (IMG); Img.onload= function() {url.revokeobjecturl (URL); $('#previewImg'). Empty (). append ($img); }; } //way to realize 2:filereader way functionpreview2 (file) {varReader= NewFileReader (); Reader.onload= function(e) {var$img= $(''). attr ("src", E.target.result); $('#previewImg'). Empty (). append ($img); }; Reader.readasdataurl (file); } $(function() { //jquery Implementation Image Click to display $(". Thumbs a"). Click (function() { varLargepath= $( This). attr ("href"); varLargealt= $( This). attr ("title"); $("#largeImg"). attr ({src:largepath, alt:largealt}); return false; }); //implementation of the picture preview function: Preview1 using URL method, Preview2 using FileReader method to implement $('[Type=file]'). Change (function(e) {varfile=e.target.files[0]; Preview2 (file); }); }); </Script> </Head> <Body> <formAction="">Please select a picture:<inputID= "MyFile"name= "MyFile"type= "File" /> <DivID= "Previewimg"></Div> </form> <HR> <H1>Picture preview</H1> <P><imgID= "Largeimg"src= "Images/img1-lg.jpg"alt= "Large Image" /></P> <Pclass= "Thumbs"> <ahref= "Images/img2-lg.jpg"title= "Image2"><imgsrc= "Images/img2-thumb.jpg"></a> <ahref= "Images/img3-lg.jpg"title= "Image3"><imgsrc= "Images/img3-thumb.jpg"></a> <ahref= "Images/img4-lg.jpg"title= "Image4"><imgsrc= "Images/img4-thumb.jpg"></a> <ahref= "Images/img5-lg.jpg"title= "Image5"><imgsrc= "Images/img5-thumb.jpg"></a> <ahref= "Images/img6-lg.jpg"title= "Image6"><imgsrc= "Images/img6-thumb.jpg"></a> </P> </Body></HTML>
Common.css file
Body{margin:20px Auto;padding:0;width:580px;Font:75%/120% Arial, Helvetica, Sans-serif;text-align:Center;}H2{Font:Bold 190%/100% Arial, Helvetica, Sans-serif;margin:0 0.2em;}H2 em{Font:normal 80%/100% Arial, Helvetica, Sans-serif;Color:#999999;}#largeImg{Border:Solid 1px #ccc;width:550px;Height:400px;padding:5px;}. Thumbs img{Border:Solid 1px #ccc;width:100px;Height:100px;padding:4px;}. Thumbs img:hover{Border-color:#FF9900;}#large{position:Absolute;Z-index:999;}
Url.revokeobjecturl Method Opera does not support, FileReader except IE9 and below does not support, other browsers are supported.
HTML5 Implement picture preview function