您需要將圖像加載到臨時img元素中,然后在臨時元素加載圖像后將其轉(zhuǎn)換為base64。 將圖像用作img.src=(YourImageSource) 然后可以從onload函數(shù)中檢索base64值。 var imgEl = document.createElement('img');imgEl.onload = function(){ var canvas = document.createElement('canvas'); canvas.width = this.width; canvas.height = this.height; var ctx = canvas.getContext('2d'); ctx.drawImage(this,0,0); const d = canvas.toDataURL('image/jpeg'); console.log('base64',d);};imgEl.src = img;