火狐与IE上传预览

IE 路径可以直接获取,但是火狐不可以,因为涉及到安全问题,火狐禁止了直接获取客户端全路径,但可以使用以下方式获取加密后路径,火狐浏览器会自动解密(自我认为)  使用struts情况下上传只需在Action 声明个File 参数get set 接收
upload:为file对象
iconPath = window.URL.createObjectURL(upload.files[0]);


//显示预览图
function showPic(iconPath){
    
    var width = '200px';
    var height = '100px';
                
   //获取HTTP浏览器的类型  
   var Browser_Agent=navigator.userAgent;
   var isFirefox = Browser_Agent.indexOf("Firefox")!=-1?true:false;
   
    if(isFirefox)
    {
      //火狐浏览器
      document.getElementById("Preview").innerHTML = "<img id='imgPreview' src='"+iconPath+"' width='"+width+"' height='"+height+"'/>";
    }
    else
    {
         //IE浏览器
      var Preview = document.getElementById('Preview');
      Preview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = iconPath;
      Preview.style.width = width;
      Preview.style.height = height;
    }  
}



function isPicSize(iconPath,upload){
    
    if(iconPath != ""){
        var extStart=iconPath.lastIndexOf("."); 
        var ext=iconPath.substring(extStart,iconPath.length).toUpperCase(); 
        if(ext!=".BMP"&&ext!=".PNG"&&ext!=".GIF"&&ext!=".JPG"&&ext!=".JPEG"){ 
            alert("照片限于bmp,png,gif,jpeg,jpg格式,请您重新上传照片"); 
            imgsizeFlag = false;
            return false; 
           } 
       
       //获取HTTP浏览器的类型  
        var Browser_Agent=navigator.userAgent;
        var isFirefox = Browser_Agent.indexOf("Firefox")!=-1?true:false;
        if(isFirefox)
        { 
        //火狐 暂时未取得全路径
          iconPath = window.URL.createObjectURL(upload.files[0]);
        }
       var imgsize = parseInt(ajaxAction());
       if(imgsize > 20*1024)//限制文件20K
       {
            alert("当前图片超过20K,请重新上传!");
            imgsizeFlag = false;
            return false;
       }
       else
       {
         showPic(iconPath);
       }
       
    }
}

//获取图片上传大小   图片大小不可获取,只有获取图片尺度,大小改为后台验证
function ajaxAction(iconPath)
{    
    var result="";
    var urlstr = "systemlink!picsize.action";
    $.ajax({
         url:urlstr,
         async:false,  //线程同步为 false,异步为true   为了保持return 能获取到值而不是空所以,要求线程同步
         dataType:"text",
         error:function(e){alert("请联系管理员,错误:"+e.message);},
         success:function(data){ result = data;}
    });
    return result;
}




<input type="file" id="uploading" name="myFile" onchange="isPicSize(this.value,this)" />

你可能感兴趣的:(IE)