【小(xiǎo)編推薦】通(tōng)過JS實  現(xiàn)上(shàng)傳圖片前的(de)本地(dì)預覽功能(nénλδg)

2016-06-13   | &nb"♠'sp; 發布者:梁國(guó)芳 &nbs‍•♦p; |   查看(kàn✔™₹"):3320次

前端開(kāi)發
 上(shàng)傳圖片前本地(dì)預覽
相(xiàng)關代碼:
注意:本文(wén)件(jiàn)需要(yào)引入₽γbs框架中的(de)模态框
<!doctype html>
<html lang="en"&∞£★gt;
<head> 
<meta charset="UTF-8←↓">
<title>JS本地(dìσ<)圖片預覽</title>&n​‌✘φbsp;
<link rel="stylesheet&qu♣γβ&ot; href="./BS/css/boφ®otstrap.min.css" /σ©'≈>
<script src="./BS/js/jq♠φ★uery.min.js"></script>
<script src="./Bs/js/boo↑♣←≤tstrap.min.js"><≥δ/script>
<style>
.modal-content{
width:250px;
height:265px;
}
.modal-dialog{
width:250px;
height:265px;
}
.modal-header{
border-bottom:none;
}
#portrait{
margin-top:5px;
margin-left:10px;
}
</style>
</head> 
<body> 
<input type="file&quo≤↑t; name="file"&quo✔≤↔t;showPreview(this)" i∞€‍≈d="file" />
<p"show()" id€↕="show" data-toggle="mod₩&₩al" data-target=&≠β<↓quot;#myModal">預覽</p&πδgt;
<p"del()" id="del&qφ ‍uot;>删除</p>
<!-- Modal -->
<div class="modal fade&quo₽×≠t; id="myModal" tabindex=&quo•∞t;-1" role="♣§Ω≤;dialog" aria-label≤₩ledby="myModalLabel" ariaα§≥-hidden="true">
 <div class="modaδ☆€l-dialog">
   <div class=&Ω↓∏quot;modal-content">
     <div class≤÷φ="modal-header&qu•Ω→ot;>
       ≤α<button type="button" clφ>​ass="close" data-dismiss=&q♥∞≈​uot;modal" aria-hid±₩‍ den="true">&times;<↔®;/button>
       ∏∑¥↔;<img id="portrait" src=₩δ"">  
     </div☆<→>
   </div±♥♣∏><!-- /.modal-content -->
 </div><!-- /.modal-d¥<±ialog -->
</div><!-- /.modal -->
</body> 
<script>
function showPreview↕∞‍(source) {  
       ≈₩;     var file = sou≠ ∏←rce.files[0]; //定義一(yī)↔★£ 個(gè)文(wén)件(jiàn)對(duì)象
          &nb ∞ sp; if(window.FileReader) {  
          &σ≤↑♦nbsp;     var≥÷¥ fr = new FileReader();  
          &n≈©bsp;     fr.onloadend = function(e) {
      φδ☆         if(!☆φ/image\/\w+/.test(file.type)){ &nbs♥"≥§p;
         β₩φ;       &n♣↑÷bsp;  return false; &≤™nbsp;
          & ×γ™nbsp;    } ✘₽  
      &★∑₩nbsp;         &nβ₽"bsp;   // document.getElementById("porσ<trait").src = e.target.result; &×←nbsp;//e.target.result代表圖片本地(dì)路(lùα←✘)徑
          &₩<nbsp;       &nbsσ♥™p; src=e.target.result"";
        &πβ®≠nbsp;      →β; };  
          &nbs‍→‌p;     fr.readAsDa&€←δtaURL(file);  
         ↓∞≠&;   }  
        }
function show(){
obj=document.getElementById("p&→ortrait");
obj.src=src;
obj.style.width="200px&↕×↕λquot;;
}
function del(){
obj=document.getElementById("portrait&q​>β÷uot;);
obj.src="";
obj.style.width="×☆π®";
obj2=document.getElementById("f •←↕ile");
obj2.value="";
}
</script>
</html>