首页 > chrome upload file 出现 fakepath,请解决方案

chrome upload file 出现 fakepath,请解决方案

用chrome上传图片,在浏览器的开发者模式中,看到图片地址被改成file:\c:\fakepathxxx.jpg了,网上搜了很多,都说的很模糊,而且没能解决这个问题,哪位大神帮忙解决一下,拜谢!


自己倒腾了半天,终于解决了,并在chrome浏览器上验证可行,方法如下:
function getImgURL(node) {

    var imgURL = "";
    try{
        var file = null;
        if(node.files && node.files[0] ){
            file = node.files[0];
        }else if(node.files && node.files.item(0)) {
            file = node.files.item(0);
        }
        //Firefox 因安全性问题已无法直接通过input[file].value 获取完整的文件路径
        try{
            //Firefox7.0
            imgURL =  file.getAsDataURL();
            //alert("//Firefox7.0"+imgRUL);
        }catch(e){
            //Firefox8.0以上
            imgURL = window.URL.createObjectURL(file);
            //alert("//Firefox8.0以上"+imgRUL);
        }
    }catch(e){      //这里不知道怎么处理了,如果是遨游的话会报这个异常
        //支持html5的浏览器,比如高版本的firefox、chrome、ie10
        if (node.files && node.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                imgURL = e.target.result;
            };
            reader.readAsDataURL(node.files[0]);
        }
    }
    return imgURL;
}

注意在传递node的时候要传递document.getElementById 获取的节点,不要用jquery的方式去获取节点

【热门文章】
【热门文章】