我从API获取字节数组,如var byteArr = [12,-123,43,99,...],然后我将其转换为UTF-8字符串
var utf8_str = String.fromCharCode.apply([], new Uint8Array(byteArr));
Run Code Online (Sandbox Code Playgroud)
然后将UTF-8字符串转换为Base64字符串
var base64_str= window.btoa(utf8_str);
Run Code Online (Sandbox Code Playgroud)
现在我在Phonegap中通过FileWriter将UTF-8或Base64字符串写入文件(xyz.pdf/xyz.jpg),但是在打开它时会显示空白文件.
function gotWriteFile(dirEntry) {
dirEntry.getFile(FILE_NAME, {create: true, exclusive: false}, gotFileWriteEntry, failWrite);
}
function gotFileWriteEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, failWrite);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
console.log("File write successfully....");
hideModal();
};
writer.write(utf8_str);
//writer.write(base64_str);
}
Run Code Online (Sandbox Code Playgroud)
什么是解决方案的家伙......?