jquery驗證File選擇文件是否為圖片


1、Html代碼

 <input type="file" id="fileimg"  onchange="fileReader(this)" value="選擇圖片" />

2、jquery代碼

function fileReader(obj) 
{
var file = obj.files; var img = document.getElementById("img"); var reader = new FileReader();// 操作圖片 if (/image/.test(file[0].type)) { reader.readAsDataURL(file[0]); } else { alert('請選擇圖片!'); obj.value = ""; return; } // 圖片加載錯誤 reader.onerror = function () { document.write("圖片加載錯誤"); } // 圖片加載完成 reader.onload = function () { img.src = reader.result; } };

 


免責聲明!

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



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