HTML5 Picture Preview

Source: Internet
Author: User

Snandy

Two ways of achieving

    • Url
    • FileReader
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 <!DOCTYPE HTML>        <meta charset="utf-8">    <title>html5 图片上传预览</title>    <style>        #preview {            width: 300px;            height: 300px;            overflow: hidden;        }        #preview img {            width: 100%;            height: 100%;        }    </style>    <script src="../jquery/jquery-1.8.3.js"></script>    <script type="text/javascript">        functionpreview1(file) {            varimg = newImage(), url = img.src = URL.createObjectURL(file)            var$img = $(img)            img.onload = function() {                URL.revokeObjectURL(url)                $(‘#preview‘).empty().append($img)            }        }        functionpreview2(file) {            varreader = newFileReader()            reader.onload = function(e) {                var$img = $(‘‘).attr("src", e.target.result)                $(‘#preview‘).empty().append($img)            }            reader.readAsDataURL(file)        }                $(function() {            $(‘[type=file]‘).change(function(e) {                varfile = e.target.files[0]                preview1(file)            })        })    </script><body><form enctype="multipart/form-data"action=""method="post">    <input type="file"name="imageUpload"/>    <div id="preview"style="width: 300px;height:300px;border:1px solid gray;"></div></form></body>

  

Url.revokeobjecturl Method Opera does not support, FileReader except IE9 and below does not support, other browsers are Supported.

Related:

Https://developer.mozilla.org/en-US/docs/DOM/window.URL.createObjectURL

Https://developer.mozilla.org/en-US/docs/DOM/window.URL.revokeObjectURL

HTML5 Picture Preview

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.