javascript 如何将 UTF-8 或 Base64 数据写入 Phonegap 本地存储(sdcard)上的文件(jpg/doc/pdf)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19657001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write UTF-8 or Base64 data into a file (jpg/doc/pdf) on local storage(sdcard) in Phonegap
提问by Saurabh
I am getting byte array like var byteArr=[12,-123,43,99, ...] from API, Then I am converting it into UTF-8 String by
我从 API 获取像 var byteArr=[12,-123,43,99, ...] 这样的字节数组,然后我将它转换为 UTF-8 字符串
var utf8_str = String.fromCharCode.apply([], new Uint8Array(byteArr));
Then converting UTF-8 string to Base64 string by
然后将 UTF-8 字符串转换为 Base64 字符串
var base64_str= window.btoa(utf8_str);
Now I am writing UTF-8 or Base64 string to file (xyz.pdf/xyz.jpg) by FileWriter in Phonegap, but it show me blank file when open it.
现在我正在 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);
}
What is solution guys.... ?
什么是解决方案家伙....?
回答by Saurabh
I have found solution to create file by byte array in Phonegap.
我找到了在 Phonegap 中按字节数组创建文件的解决方案。
In phonegap, Text and Binary data are supported for Android and iOS to write into file. So I have convert BYTE array to BINARY array, then write by FileWriter.
在phonegap 中,Android 和iOS 支持Text 和Binary 数据写入文件。所以我已将 BYTE 数组转换为 BINARY 数组,然后由 FileWriter 写入。
var byteArr=[12,-123,43,99, ...] ;
var UTF8_STR = new Uint8Array(byteArr); // Convert to UTF-8...
var BINARY_ARR=UTF8_STR.buffer; // Convert to buffer...
Then pass 'BINARY_ARR' to FileWriter to write in file.
然后将 'BINARY_ARR' 传递给 FileWriter 以写入文件。
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
console.log("File write successfully....");
};
writer.write(BINARY_ARR);
}
Have a nice day.. :)
祝你今天过得愉快.. :)
回答by Vicky Gonsalves
try this (make sure u are getting utf8_str properly):
试试这个(确保你正确获取 utf8_str):
var utf8_str = String.fromCharCode.apply([], new Uint8Array(byteArr));
var base64_str= window.btoa(utf8_str);
function writeFile() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile(FILE_NAME, {create: true}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function (evt) {
alert('done');
}
writer.write(utf8_str);
}
function fail(error) {
console.log(error.code);
}
回答by gaqzi
I had issues on another project with btoa
not doing a proper conversion, so I ended up using Base64 binaryin my project to decode the Base64 response.
我在另一个项目中遇到了btoa
没有正确转换的问题,所以我最终在我的项目中使用Base64 二进制文件来解码 Base64 响应。
What I noticed was that the file I transferred was bigger when saved on the phone compared to the file residing on the server.
我注意到的是,与驻留在服务器上的文件相比,我传输的文件在手机上保存时更大。
Maybe that is the case for you as well?
也许你也是这种情况?