我正面临一个关于从 Rest 服务解码 response.responseText 的问题(该工作是向我发送一个 cert.p12 文件)。
所以我能够得到我的数据
var obj = Ext.JSON.decode(response.responseText);
那么这个数据是一个base64对象,我想解码并写入我的文件。
我尝试了几个 javascript 解码器但没有运气,cordova writer.write 方法不起作用,而尝试编写我的文件但没有解码方法有效(但不是有效文件)。
有人可以帮我找到一种方法来解码我的文件吗?这是我打给其余服务的电话
Ext.Ajax.request({
url: 'https://certtest.idshost.fr/restgetcertificate.php',
method: 'POST',
withCredentials: false,
useDefaultXhrHeader: false,
params: Ext.JSON.encode(data),
callback: function(options, success, response) {
if (success) {
var obj = Ext.JSON.decode(response.responseText);
localStorage.setItem('certif', obj.certbase64encoded);
} else{
Ext.Msg.alert('Fail', 'marche pas');
}
}
});
文件 API 函数
toto:function(obj){
console.log('entree toto');
var ba64 =obj.certbase64encoded;
var decode = decode64(ba64);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 5*1204*1024, gotFS, fail);
function gotFS(fileSystem) {
console.log("getfile");
fileSystem.root.getFile("cert.p12", {create: true}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
console.log("create file");
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.write(decode);
}
}
function fail(error) {
console.log(error.code);
}
}
我的解码器功能
function decode64(input) {
var keyStr = "ABCDEFGHIJKLMNOP" +
"QRSTUVWXYZabcdef" +
"ghijklmnopqrstuv" +
"wxyz0123456789+/" +
"=";
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
var base64test = /[^A-Za-z0-9\+\/\=]/g;
if (base64test.exec(input)) {
alert("There were invalid base64 characters in the input text.\n" +
"Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
"Expect errors in decoding.");
}
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
do {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return unescape(output);
}