Html5上传图片的预览

<html>
<head>
    <script type="text/javascript" src="jquery-1.6.2.js"></script>
    <script type="text/javascript" src="html5.js"></script>
    <script>

        $(function(){
            $('#importFile').change(function(){
                var files = this.files ? this.files: [];
                
                if(!files.length ) {
                
                    return;
                }
                if(typeof FileReader == undefined) return;
                
                if(/^image/.test(files[0].type)){
                
                    var reader = new FileReader();
                    
                    reader.readAsDataURL(files[0]);
                    
                    reader.onloadend = function(){
                    
                        $('#image1').attr('src',this.result);
                    }
                }
            })
            
            $('#image1').click(function(){
                
                $('#importFile').click();
            })
        });
        
    </script>
</head>
<body>
    <img id="image1" style="width:100px;height:100px" />
    <div>
        <input type="file" id="importFile" style="display:none" />
    </div>
</body>
</html>

 

posted @ 2014-04-03 17:19  Kyrie.Xu  阅读(246)  评论(0编辑  收藏  举报