2016-08-08 20 views
0

我將我的文件名保存到數據庫中,並將文件存儲在文件服務器中。所以我可以通過獲取名稱來訪問該文件並執行<img src='http://localhost/upload/abc.jpg'/>,其中abc是文件名。但是,我如何獲得base64?因爲我需要獲取實際的文件並將其傳遞到某處。通過文件名獲得base64以查看

+2

這將是服務器端的代碼,而不是客戶端。你究竟在做什麼? – Archer

+0

嘗試看看http://stackoverflow.com/questions/19124701/get-image-using-jquery-ajax-and-decode-it-to-base64/25371174#25371174 – gaetanoM

回答

0

如果圖像是在同一個域中,可以用帆布做的..

function getBase64Image(img) { 
    var canvas = document.createElement("canvas"); 
    canvas.width = img.width; 
    canvas.height = img.height; 
    var ctx = canvas.getContext("2d"); 
    ctx.drawImage(img, 0, 0); 
    return canvas.toDataURL("image/png"); 
} 

// if the image is on the dom use this 
var img = document.getElementById('img'); 
alert(getBase64Image(img)); 

// if the image isn't on the dom do this.. 
var img = new Image(); 
img.onload = function(){ 
    alert(getBase64Image(img)); 
} 
img.src= "http://my/image/url.png"; 
+0

非常感謝! –