how to preview an image before upload in various browsers

后端 未结 5 990
遥遥无期
遥遥无期 2020-12-29 15:25

I want to show preview of an image before it is uploaded. I have found a partial solution that works for ie6 and firefox, and havent yet tested it in ie7 or ie8. But i want

5条回答
  •  醉梦人生
    2020-12-29 16:13

    you can use blow function. tested on IE7+ and Firefox and chrome

    function loadname(img, previewName){  
    
    var isIE = (navigator.appName=="Microsoft Internet Explorer");  
    var path = img.value;  
    var ext = path.substring(path.lastIndexOf('.') + 1).toLowerCase();  
    
     if(ext == "gif" || ext == "jpeg" || ext == "jpg" ||  ext == "png" )  
     {       
        if(isIE) {  
           $('#'+ previewName).attr('src', path);  
        }else{  
           if (img.files[0]) 
            {  
                var reader = new FileReader();  
                reader.onload = function (e) {  
                    $('#'+ previewName).attr('src', e.target.result);  
                }
                reader.readAsDataURL(img.files[0]);  
            }  
        }  
    
     }else{  
      incorrect file type  
     }   
    }  
    
    
    
    

提交回复
热议问题