0
function convertImgToBase64(url){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var img = new Image;
img.crossOrigin = 'Anonymous';
img.onload = function(){
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img,0,0);
var dataURL = canvas.toDataURL('image/png',"");
alert(dataURL);
canvas = null;
};
img.src = url;
}
var url_to_be_converted = "http://www.google.com/image/sample"
convertImgToBase64(url_to_be_converted);
Alert(dataURL)不顯示任何結果。是否在此處生成彈出窗口?如何解決這個問題?
有什麼不對?使用javascript將圖像轉換爲base64
缺少分號裝的形象呢?應該是'var url_to_be_converted =「http://www.google.com/image/sample」;' – tillz
不,這不是問題 – user3858217
@tillz:分號在JavaScript中是可選的,儘管它們的省略有時會導致意想不到的問題。 –