html5圖片上傳預(yù)覽示例分享

字號:


    代碼如下:
    <!doctype html>
    <html lang=zh-cn>
    <head>
    <meta charset=utf-8 />
    <meta name=author content=edielei />
    <title>html5 圖片上傳預(yù)覽</title>
    <script type=text/javascript src=<a href=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script>http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script</a>>
    <script type=text/javascript>
    $(function(){
    $('#img').change(function(){
    var file = this.files[0]; //選擇上傳的文件
    var r = new filereader();
    r.readasdataurl(file); //base64
    $(r).load(function(){
    $('div').html('<img src='+ this.result +' alt= />');
    });
    });
    });
    </script>
    </head>
    <body>
    <h3>html5 圖片上傳預(yù)覽</h3>
    <input id=img type=file accept=image/* />
    <div></div>
    </body>
    </html>