js获取input file完整路径的方法

function getPath()
{
  //判断浏览器
  var Sys = {};
  var obj = document.getElementById("headImg");
  var viewer = document.getElementById("viewImg");
  var ua = navigator.userAgent.toLowerCase();
  var s;
  (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
  (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
  (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
  (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
  (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
  var path="";
  if(Sys.ie<="6.0"){
    //ie5.5,ie6.0
    viewer.src = obj.value;
  }else if(Sys.ie>="7.0"){
    //ie7,ie8
    obj.select();
    //加上这一句防止报告安全问题
    obj.blur();
    viewer.src = document.selection.createRange().text;
  }else if(Sys.firefox){
    if (obj.files) {
        var accept = ["image/png","image/jpeg","image/jpg","image/gif"];
        if (accept.indexOf(obj.files[0].type) > -1) {
            var reader = new FileReader();
            reader.readAsDataURL(obj.files[0]);
            //延迟一会等待文件读取完毕
            var t = setTimeout(function(){
            viewer.src = reader.result;
            clearTimeout(t);
            },100)
        }
    }
    else
    {
        viewer.src = obj.value;
    }
  }else if(Sys.chrome){
    viewer.src = obj.value;
  }
}

<form>
<input type="file" id="headImg" />
<input id="btnOk" type="button" value="ok" onclick="getPath()"/>
<img src="http://blog.163.com/zhchf_52@yeah/blog/" alt="view" id="viewImg" />
</form>

由于IE9不能实时的显示图片,所以采用滤镜的方法
<style type="text/css">
#viewImg2{ /* 该对象用于在IE下显示预览图片 */    
 filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);    
 width:150px;
 height:150px;
 display:none;
 overflow: hidden;
}    
#viewImg{ /* 该对象只用来在IE下获得图片的原始尺寸,无其它用途 */    
  filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);   
  width:150px;
  height:150px;
  display:none;
  overflow:hidden;
}
</style>

<form id="uploadForm" action="" method="post" enctype="multipart/form-data">
<input type="file" id="headImg" onchange="view()" name="headImg"/>
<input type="submit" value="保存" id="btnSave" disabled="disabled" />
<p class="lineH30 color6">建议使用正方形的图片,支持JPG、GIF、PNG格式,100K以内。</p>
<div id="viewImg2"></div>
<img src="http://blog.163.com/zhchf_52@yeah/blog/" alt="" id="viewImg"/>
<script type="text/javascript">
    //file文件控件,viewer 图像查看,accept支持的文件格式的mimetype数组
    function viewImage(file, viewer, viewer2, btn, accept) {
        //判断浏览器
        var Sys = {};
        if (file && viewer) {
            if (!viewer2) {
                viewer2 = viewer;
            }
            if (!accept || accept.length == 0) {
                accept = ["image/png", "image/jpeg", "image/jpg", "image/gif", "image/bmp"];
            }
            var ua = navigator.userAgent.toLowerCase();
            var s;
            (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
            (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
            (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
            (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
            (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
            var path = "";
            if (Sys.ie) {
                viewer.style.display = "none";
                if (Sys.ie <= "6.0") {
                    //ie5.5,ie6.0
                    path = file.value;

                }
                else if (Sys.ie >= "7.0") {
                    //ie7,ie8
                    file.select();
                    //加上这一句防止报告安全问题
                    file.blur();
                    path = document.selection.createRange().text;
                }
                v = isValidImage(path, accept);
                if (v) {
                    viewer2.style.display = "block";
                    if (viewer2.filters) {
                        viewer2.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = path;
                    }
                    else {
                        viewer2.src = path;
                    }
                }
            }
            else if (Sys.firefox) {
                viewer2.style.display = "none";
                viewer.style.display = "block";
                if (file.files && file.files.length > 0) {
                    if (accept.indexOf(file.files[0].type) > -1) {
                        var reader = new FileReader();
                        reader.readAsDataURL(file.files[0]);
                        //延迟一会等待文件读取完毕
                        var t = setTimeout(function () {
                            viewer.src = reader.result;
                            clearTimeout(t);
                        }, 100);
                        v = true;
                    }
                }
                else {
                    path = file.value;
                    v = isValidImage(path, accept);
                    if (v) {
                        viewer.src = file.value;
                    }
                }
            }
            else {
                viewer2.style.display = "none";
                viewer.style.display = "block";
                path = file.value;
                v = isValidImage(path, accept);
                if (v) {
                    viewer.src = file.value;
                }
            }

            if (v) {
                if (btn) {
                    $("#" + btn).removeAttr("disabled");
                }
            }
            else {
                viewer.src="http://blog.163.com/zhchf_52@yeah/blog/";
                $("#btnSave").attr("disabled", "disabled");
            }
        }
    }
    //是否为有效的图片格式
    function isValidImage(path, accept) {
        var v = false;
        if (path
        && path.length > 0
        && path.indexOf(".") > 0
        && accept
        && accept.length > 0) {
            var idx = path.lastIndexOf(".") + 1;
            if (path.length > idx) {
                var ext = path.substr(idx).toLowerCase();
                for (var i = 0; i < accept.length; i++) {
                    if (accept[i].indexOf("/" + ext) > 0) {
                        v = true;
                        break;
                    }
                }
            }
        }
        return v;
    }

    function autoSizePreview(objPre, originalWidth, originalHeight) {
        var zoomParam = clacImgZoomParam(150, 150, originalWidth, originalHeight);
        objPre.style.width = zoomParam.width + 'px';
        objPre.style.height = zoomParam.height + 'px';
        objPre.style.marginTop = zoomParam.top + 'px';
        objPre.style.marginLeft = zoomParam.left + 'px';
    }

    function clacImgZoomParam(maxWidth, maxHeight, width, height) {
        var param = { width: width, height: height, top: 0, left: 0 };

        if (width > maxWidth || height > maxHeight) {
            rateWidth = width / maxWidth;
            rateHeight = height / maxHeight;

            if (rateWidth > rateHeight) {
                param.width = maxWidth;
                param.height = height / rateWidth;
            } else {
                param.width = width / rateHeight;
                param.height = maxHeight;
            }
        }

        param.left = (maxWidth - param.width) / 2;
        param.top = (maxHeight - param.height) / 2;

        return param;
    }

    function view() {
        viewImage(document.getElementById("headImg"),
        document.getElementById("viewImg"),
        document.getElementById("viewImg2"),
        "btnSave",
        accept);
    }
</script>
</form>

你可能感兴趣的:(input)