I've been working on a plugin specifically for Cordova 3.3.
Within my JavaScript code, I'm in the process of downloading a PDF file which is being stored as a uInt8Array.
download: function ( ) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'url/to/pdf', true);
xhr.responseType = 'arraybuffer';
xhr.setRequestHeader('Accept', 'application/pdf');
xhr.onload = function(e) {
var uInt8Array = new Uint8Array(this.response);
window.plugins.filedownloader.saveFile(
uInt8Array,
"Downloads",
function ( response ) {
console.log("success");
},
function ( response ) {
console.log("failure");
}
);
};
xhr.send();
},
I pass the uInt8Array to the save method of my Cordova plugin. The JavaScript portion of my plugin appears as follows:
var exec = require('cordova/exec');
function FileDownloader() {
};
FileDownloader.prototype = {
login: function ( loginUrl, params, username, password, successCallback, failureCallback ) {
exec( successCallback, failureCallback, "FileDownloader", "doLogin", [ loginUrl, params, username, password ] );
},
getHtmlPage: function ( url, successCallback, failureCallback ) {
exec( successCallback, failureCallback, "FileDownloader", "getHtmlPage", [ url ] );
},
downloadAndSaveFile: function ( url, saveFolder, successCallback, failureCallback ) {
exec( successCallback, failureCallback, "FileDownloader", "downloadAndSaveFile", [ url, saveFolder ] );
},
saveFile: function ( data, saveFolder, successCallback, failureCallback ) {
exec( successCallback, failureCallback, "FileDownloader", "saveFile", [ data, saveFolder ] );
}
};
FileDownloader.install = function () {
if (!window.plugins) {
window.plugins = {}
}
window.plugins.filedownloader = new FileDownloader();
return window.plugins.filedownloader;
};
cordova.addConstructor(FileDownloader.install);
The save method within the Java section is as such:
public PluginResult execute(JSONArray args) {
PluginResult result = new PluginResult(PluginResult.Status.OK, "blub");
log("saveFile");
try{
JSONObject dataValues = args.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
Now, the issue arises where the typedArray transforms into a JSONObject when passing it over. The indices of the uInt8Array become strings and are no longer ordered. Additionally, a new key "byteBuffer" is present.
Is there a method to handle a typedArray as a JSONArray when transferred to the Java part of the plugin, or should I explore an alternative solution to address this challenge?