通过 html5 FileReader 实现上传图片预览功能

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

Html 部分

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>

    <body>

        <input type="file" name="file" onchange="showPreview(this)" />
        <img id="portrait" src="" width="70" height="75">
    </body>

</html>



JS部分

<script type="text/javascript">
    function showPreview(source) {
        var file = source.files[0];
        if (window.FileReader) {
            var fr = new FileReader();
            fr.onloadend = function(e) {
                document.getElementById("portrait").src = e.target.result;
            };
            fr.readAsDataURL(file);
        }
    }
</script>