• 原生js上传图片时的预览


    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>checkbox -value </title>
        <link rel="stylesheet" href="">
        <script src='./jquery-1.7.1.min.js'></script>
        <style type="text/css">
        .showImg {}
    
        .showImg img {
            width: 50px;
            height: 50px;
        }
        </style>
    </head>
    <body>
        <div class="main">
            <label for="imgfile"><span class="fakeButton">Insert Images</span></label>
            <!-- 下面为了避免中文,由上面替换 -->
            <!-- <input type="file" accept="image/jpeg,image/png,image/gif" name="imgfile" id="imgfile" multiple value="Insert Image" onchange="javascript:setImagePreview();" style="position:absolute;clip:rect(0 0 0 0);"> -->
            <input type="file" accept="image/jpeg,image/png,image/gif" name="imgfile" id="imgfile" multiple onchange="javascript:setImagePreview();">
            <br />
            <div id="showImg" class="showImg"></div>
        </div>
        <script type="text/javascript">
        function setImagePreview() {
            var imghtml = imgUrls();
            if (imghtml === '111') {
                window.alert('Notice', 'You can upload up to three photos');
                return false;
            }
            $('#showImg').html(imghtml);
        }
    // 限制传递图片的张数
        function imgUrls() {
            var imgsrcs = getUrl();
            var imghtml = '';
            var len = imgsrcs.length;
            if (len > 3) {
                return '111';
            }
            for (var i = 0; i < len; i++) {
                imghtml += '<img src=' + imgsrcs[i] + ' alt="you have selected">&nbsp;&nbsp;';
            }
            return imghtml;
        }
    
        function getUrl() {
            var obj = $('#imgfile')[0].files;
            var len = obj.length;
            var imgsrcs = [];
            for (var i = 0; i < len; i++) {
                imgsrcs[i] = getObjectURL(obj[i]);
            }
            //添加图片路径到img src中进行预览
            //不同浏览器下的路径不同
            function getObjectURL(file) {
                var url = null;
                if (window.createObjectURL != undefined) { // basic
                    url = window.createObjectURL(file);
                } else if (window.URL != undefined) { // mozilla(firefox)
                    url = window.URL.createObjectURL(file);
                } else if (window.webkitURL != undefined) { // webkit or chrome
                    url = window.webkitURL.createObjectURL(file);
                }
                return url;
            }
    
            return imgsrcs;
        }
        </script>
    </body>
    
    </html>
  • 相关阅读:
    献给所有学习控件和组件开发的读者
    ASP.NET 2.0书稿最新进展——开始一审
    愿我们共同进步——献给《Programming ASP.NET 3rd. 中文版》
    Error from MSDN——No4. Login.UserName
    安装MSDN中文版后的乱弹
    Error from MSDN——No9. Login.UserNameRequiredErrorMessage
    Error from MSDN——No1. Roles.Enabled
    Error from MSDN——No8. Login.PasswordRequiredErrorMessage
    热心的朋友请注意:ASP.NET 2.0书稿目录征求意见
    撰写《ASP.NET 2.0开发指南》的非正式小结
  • 原文地址:https://www.cnblogs.com/xhliang/p/7754386.html
Copyright © 2020-2023  润新知