jquery html5 file 上傳圖片顯示圖片(上傳圖片預覽)


 

// html代碼

<input type="file" name="path[]" id="file0"  />

<img class="coverPicture" src="" id="img0" style="height:150px; width:250px;  display: none">

 

//建立一個可存取到該file的url

// jquery js 的代碼:不同瀏覽器下的路徑

function getObjectURL(file) {

var url = null ;

if (window.createObjectURL!=undefined) { // basic

url = window.createObjectURL(file) ;

} else if (window.URL!=undefined) { // mozilla(firefox)

url = window.URL.createObjectURL(file) ;

} else if (window.webkitURL!=undefined) { // webkit or chrome

url = window.webkitURL.createObjectURL(file) ;

}

return url ;

}

 

//顯示圖像 預覽效果

$("#file0").change(function(){
  var objUrl = getObjectURL(this.files[0]) ;
  console.log("objUrl = "+objUrl) ;
if (objUrl) {
  $("#img0").attr("src", objUrl) ;
  $("#img0").show() ;
}

}) ;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM