<script> // 圖片地址 轉為 blob 、base64格式 imgUrl型別為字串string function base64ByURL (imgUrl) { // 兩大重點 Promise XMLHttpRequest return new Promise(resolve => { var xhr = new XMLHttpRequest(); xhr.open("get", imgUrl, true); xhr.responseType = "blob"; xhr.onload = function () { if (this.status == 200) { let blob = this.response; let oFileReader = new FileReader(); oFileReader.onloadend = function (e) { resolve({blob,base64:e.target.result}) }; oFileReader.readAsDataURL(blob); } }; xhr.send(); }); }; // 呼叫 base64ByURL().then(res=>{ let {blob,base64} = res; console.log(blob,base64,"blob,base64") }); </script>