javascript - chrome upload file 出现 fakepath,请解决方案
天蓬老师
天蓬老师 2017-04-11 11:51:41
0
3
1426

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

répondre à tous(3)
巴扎黑

自己倒腾了半天,终于解决了,并在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的方式去获取节点

伊谢尔伦

感动到哭,谢谢你!

巴扎黑

获取出来的值是加了密的啊 我上传要使用的地址要怎么解析 blob:http:localhost:808077b295b4-a8b0-40f2-8523-63446f5fedfd (文件名、目录名或卷标语法不正确。) 这是通过此方法获取的文件路径,我只是要某个盘符下的某个具体文件

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!