Get the access address url of input[type=file], which can be used for local selection of picture preview

<img id="img01" src="" alt="" />


//获取file文件的访问地址
function getFileUrl(file) {
    
    
                var url;
                var agent = navigator.userAgent;
                if (agent.indexOf("MSIE") >= 1 || agent.indexOf("NET")!=-1) {
    
    
                    url = window.URL.createObjectURL(file);
                } else if (agent.indexOf("Firefox") > 0) {
    
    
                    url = window.URL.createObjectURL(file);
                } else if (agent.indexOf("Chrome") > 0) {
    
    
                    url =window.webkitURL.createObjectURL(file);
                }
                return url;
}


//调用
var file=document.getElementById('file').files[0];

//得到地址 blob:http://localhost:8050/18fe5bfa-3d31-4514-bf98-16b0606acb1b
var url = getFileUrl(file);

var imgObj = new Image();
imgObj.src = url;
imgObj.onload = function () {
    
    
   document.getElementById('img01').src=url;
   //<img src="blob:http://localhost:8050/0d9b06a8-982f-431b-aee9-bfe6840a1e24" alt="">
}

 


Guess you like

Origin blog.csdn.net/u011511086/article/details/113396810