JS上傳圖片且在線預覽,修改瀏覽器兼容性問題

[html] view plain copy
  1. <span style="font-size:14px;">    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.     <html xmlns="http://www.w3.org/1999/xhtml" >  
  3.     <head>  
  4.     <meta charset="utf-8" />  
  5.     <title>本地圖片預覽</title>  
  6.     <style type="text/css">  
  7.     #preview{width:300px;height:300px;border:1px solid #000;overflow:hidden;}  
  8.     #imghead {filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);}  
  9.     </style>  
  10.     <script type="text/javascript">  
  11.     function previewImage(file)  
  12.     {  
  13.           
  14.   
  15.       var MAXWIDTH  = 300;  
  16.       var MAXHEIGHT = 300;  
  17.       var div = document.getElementById('preview');  
  18.       if (file.files && file.files[0])  
  19.       {  
  20.         div.innerHTML = '<img id=imghead>';  
  21.         var img = document.getElementById('imghead');  
  22.         img.onload = function(){  
  23.           var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);  
  24.           img.width = rect.width;  
  25.           img.height = rect.height;  
  26.           img.style.marginLeft = rect.left+'px';  
  27.           img.style.marginTop = rect.top+'px';  
  28.         }  
  29.       
  30.         var reader = new FileReader();  
  31.         reader.onload = function(evt){  
  32.                  var type = evt.target.result.substr(0,10);  
  33.                  if(type!='data:image'){  
  34.                     alert("請選擇圖片上傳");  
  35.                     return ;  
  36.                  }  
  37.                 img.src = evt.target.result;  
  38.                 }  
  39.         reader.readAsDataURL(file.files[0]);  
  40.       }  
  41.       else  
  42.       {  
  43.         var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';  
  44.         file.select();  
  45.         var src = document.selection.createRange().text;  
  46.         var type =src.substr(src.lastIndexOf(".")+1);   
  47.          type = type.toLowerCase();  
  48.             
  49.           var reg = new RegExp("^(png|jpg|bmp|gif|jpeg)$");    
  50.         if(!reg.test(type)){   
  51.             alert("請選擇圖片上傳");  
  52.             return ;  
  53.         }  
  54.            
  55.         div.innerHTML = '<img id=imghead>';  
  56.         var img = document.getElementById('imghead');  
  57.         img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;  
  58.         var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);  
  59.         status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);  
  60.         div.innerHTML = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;margin-left:"+rect.left+"px;"+sFilter+src+"\"'></div>";  
  61.       }  
  62.     }  
  63.     function clacImgZoomParam( maxWidth, maxHeight, width, height ){  
  64.         var param = {top:0, left:0, width:width, height:height};  
  65.         if( width>maxWidth || height>maxHeight )  
  66.         {  
  67.             rateWidth = width / maxWidth;  
  68.             rateHeight = height / maxHeight;  
  69.             if( rateWidth > rateHeight )  
  70.             {  
  71.                 param.width =  maxWidth;  
  72.                 param.height = Math.round(height / rateWidth);  
  73.             }else  
  74.             {  
  75.                 param.width = Math.round(width / rateHeight);  
  76.                 param.height = maxHeight;  
  77.             }  
  78.         }  
  79.         param.left = Math.round((maxWidth - param.width) / 2);  
  80.         param.top = Math.round((maxHeight - param.height) / 2);  
  81.         return param;  
  82.     }  
  83.     </script>  
  84.     </head>  
  85.     <body>  
  86.     <div id="preview">  
  87.         <img id="imghead" width="300" height="300" border="0" src='../images/demo.jpg'><!--無預覽時的默認圖像,自己弄一個-->  
  88.     </div>  
  89.         <br/>  
  90.         <input type="file" onchange="previewImage(this)" />  
  91.     </body>  
  92.     </html>  
  93.   
  94.      </span>  
轉載:http://www.codefans.net/articles/1395.shtml
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章