zweizhao.github.io

项目,文章,随笔,博客,Markdown,个人网站,人生感悟。

View on GitHub

input(type=”file” accept=”image/*” capture=”camera” multiple) accept 限定格式 capture 可选摄像头(移动端使用,因浏览器区别而不同) multiple 可选多张文件或图片(移动端受浏览器限制)

<!DOCTYPE HTML>  
<html>  
<head>  
    <title>上传图片</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <meta charset="utf-8">  
</head>  
<body>  
    <iframe name="uploadfrm" id="uploadfrm" style="display: none;"></iframe>  
    <form name="formHead" method="post" action="" id="formHead" enctype="multipart/form-data" target="uploadfrm">  
  
        <div>  
            <div>  
                <input type="file" name="file_head" id="file_head" onchange="javascript:setImagePreview();" />  
            </div>  
            <div>  
                <div id="DivUp" style="display: none">  
                    <input type="submit" data-inline="true" id="BtnUp" value="确认上传" data-mini="true" />  
                </div>  
            </div>  
        </div>  
    </form>  
    <div data-role="fieldcontain">  
        <div id="localImag">  
            <img id="preview" width="-1" height="-1" style="display: none" />  
        </div>  
    </div>  
          
  
    <script type="text/javascript">  
        function setImagePreview() {  
            var preview, img_txt, localImag, file_head = document.getElementById("file_head"),  
            picture = file_head.value;  
            if (!picture.match(/.jpg|.gif|.png|.bmp/i)) return alert("您上传的图片格式不正确,请重新选择!"),  
            !1;  
            if (preview = document.getElementById("preview"), file_head.files && file_head.files[0]) preview.style.display = "block",  
                preview.style.width = "63px",  
                preview.style.height = "63px",  
                preview.src = window.navigator.userAgent.indexOf("Chrome") >= 1 || window.navigator.userAgent.indexOf("Safari") >= 1 ? window.webkitURL.createObjectURL(file_head.files[0]) : window.URL.createObjectURL(file_head.files[0]);  
            else {  
                file_head.select(),  
                file_head.blur(),  
                img_txt = document.selection.createRange().text,  
                localImag = document.getElementById("localImag"),  
                localImag.style.width = "63px",  
                localImag.style.height = "63px";  
                try {  
                    localImag.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)",  
                    localImag.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = img_txt  
                } catch(f) {  
                    return alert("您上传的图片格式不正确,请重新选择!"),  
                    !1  
                }  
                preview.style.display = "none",  
                document.selection.empty()  
            }  
            return document.getElementById("DivUp").style.display = "block",  
            !0  
        }  
    </script>  
</body>  
</html>