画像をエンコードしてデコードしようとしています。FileReaderのreadAsDataURLメソッドを使用して、画像をbase64に変換しています。それからそれを元に戻すために私は使ってみ ましたが運がありませんでしたreadAsBinaryString()
。atob()
base64でエンコードせずに画像を永続化する別の方法はありますか?
readAsBinaryString()
指定されたBlob(ファイルの場合もあります)の内容の読み取りを開始します。読み取り操作が終了すると、readyStateはDONEになり、onloadendコールバック(存在する場合)が呼び出されます。その時点で、result属性にはファイルからの生のバイナリデータが含まれています。
私がここで間違っていることについて何か考えはありますか?
サンプルコード 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
});
ありがとう!