HTML5 Implement picture preview function

Source: Internet
Author: User

Two ways of achieving

    • Url
    • FileReader

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

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.