JS 前端 將圖片轉換為Base64利用H5 FileReader新特性


 
file = document.getElementById("image");
var file=file.files[0];
var fileName=file.name;

if(file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
alert('當前僅支持圖片!');
return;
}
var size = Math.round(file.size / 1024 / 1024);
if (size > 2)
{
alert('圖片大小不得超過2M');
return;
}

var reader = new FileReader();
imgUrlBase64 =reader.readAsDataURL(file);
reader.onload = function(e)
{
   reader.result;//讀取到的base64文件流
  //此方法為異步方法,將調用的AJAX處理放在這個funtion里面
   ajax{
         //代碼
         }
});
}.bind(this);


免責聲明!

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



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