我正在嘗試編碼和解碼圖像。我正在使用FileReader的readAsDataURL方法將圖像轉換爲base64。然後將其轉換回來,我嘗試過使用readAsBinaryString()
和atob()
,但沒有運氣。有沒有其他的方式來保持圖像沒有base64編碼他們?帶有base64中斷圖像的編碼/解碼圖像
readAsBinaryString()
Starts reading the contents of the specified Blob, which may be a File. When the read operation is finished, the readyState will become DONE, and the onloadend callback, if any, will be called. At that time, the result attribute contains the raw binary data from the file.
任何想法我在做什麼錯在這裏?
示例代碼 http://jsfiddle.net/qL86Z/3/
$("#base64Button").on("click", function() {
var file = $("#base64File")[0].files[0]
var reader = new FileReader();
// callback for readAsDataURL
reader.onload = function (encodedFile) {
console.log("reader.onload");
var base64Image = encodedFile.srcElement.result.split("data:image/jpeg;base64,")[1];
var blob = new Blob([base64Image],{type:"image/jpeg"});
var reader2 = new FileReader();
// callback for readAsBinaryString
reader2.onloadend = function(decoded) {
console.log("reader2.onloadend");
console.log(decoded); // this should contain binary format of the image
// console.log(URL.createObjectURL(decoded.binary)); // Doesn't work
};
reader2.readAsBinaryString(blob);
// console.log(URL.createObjectURL(atob(base64Image))); // Doesn't work
};
reader.readAsDataURL(file);
console.log(URL.createObjectURL(file)); // Works
});
謝謝!
在瀏覽器這個例子中工作? TypeError:encodedFile.srcElement未定義 – cIph3r 2013-02-19 23:35:41
我正在使用Chrome進行測試,但我需要它在最新的Android和iOS瀏覽器中工作。 – sissonb 2013-02-19 23:37:20