日期:2014-05-20  浏览次数:20842 次

IE8 下本地图片浏览不了
昨天下载了IE8发现,选择本地图片,在我的浏览器上就不显示了,也上网搜了一堆相关说法,没发现有能解决这个问题的。现将我的代码公布,希望能遇到过的朋友指点。

JSP:
用的是Struts2 的标签。
<s:file name="doctorPhoto" id="doctorPhoto" 
onchange="document.getElementById('photo').src=getFullPath(this);"
cssStyle=" width:166px;" />

<img id="photo" src="<%=request.getContextPath() %>/tmp/doctorPhoto.gif" width="150" height="120" border="1" />&nbsp;&nbsp;


JS:

function getFullPath(obj){
  if(obj){
  //ie
  if (window.navigator.userAgent.indexOf("MSIE")>=1){
 
  obj.select();
  return document.selection.createRange().text;
  }
  //firefox
  else if(window.navigator.userAgent.indexOf("Firefox")>=1){
  if(obj.files){
  return obj.files.item(0).getAsDataURL();
  }
  return obj.value;
  }
  return obj.value;
  }
   
   
}

------解决方案--------------------
Java code
 
<script type="text/javascript">
    function getFullPath(divImage, upload, width, height){
      if(upload.value==""){
            return false;
        }
        
        var isImage = /\.jpg|\.jpeg|\.gif|\.png|\.bmp$/i;
        if (!isImage.test(upload.value)) {
            alert("图片格式错误,请重新选择!");
            return false;
        }
    
        var imgPath; //图片路径     
        var Browser_Agent = navigator.userAgent;
        var Preview = document.getElementById(divImage);
        Preview.innerHTML = "";
        
        if (Browser_Agent.indexOf("Firefox") != -1)//判断浏览器的类型
        {
            //火狐浏览器
            imgPath = upload.files[0].getAsDataURL();
            Preview.innerHTML = "<img id='imgPreview' src='" + imgPath
                    + "' width='" + width + "' height='" + height + "'/>";
        } else {
            if (navigator.userAgent.indexOf("MSIE 6.0") == -1
                    && navigator.userAgent.indexOf("MSIE") > -1) {
                upload.select();
                var imgSrc = document.selection.createRange().text;
                //IE7、8预览方式
    
                Preview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
                Preview.style.width = width;
                Preview.style.height = height;
            } else {
                // IE6预览方式
                Preview.innerHTML = "<img id='photo" + "' src='"
                        + upload.value + "' style='width:" + width + "px;height:"
                        + height + "px;' />";
            }
        }
    }
    </script>
    <body>
        <s:file name="doctorPhoto" id="doctorPhoto"
            onchange="getFullPath('Preview',this,150,120);"
            cssStyle=" width:166px;" />
        <div id="Preview" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);">
            <img id="photo"
                src="<%=request.getContextPath()%>/images/1.gif" width="150"
                height="120" border="1" />
        </div>

    </body>