Whena pdf is requested, the server generates the binary data for the pdf and spits it out to the browser with
Code:
header('Content-Length: '.strlen($this->buffer));
header('Content-disposition: attachment; filename="'.$name.'"');
which, when surfing normally with a web browser, causes one of those 'save or open with' dialogs to be displayed by the browser. With Sencha Touch, I can get a blob from the server, as follows:
Code:
var xhr = new XMLHttpRequest();
console.log('new XMLHttpRequest');
var url = 'http://bladhdeblah.com/ws/Download/';
var params = "loginId=" + loginId + "&sId=" + surveyId;
xhr.open('POST', url, true);
xhr.responseType = 'blob';
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onload = function (e) {
if (this.status == 200) {
Ext.Viewport.setMasked(false);
blob = new Blob([this.response], {type: 'application/pdf'});
console.log('new Blob');
var cd = (this.getResponseHeader("Content-disposition"));
var filenameStart = cd.indexOf("\"") + 1;
var filename = cd.substring(filenameStart, cd.length - 1);
console.log('pdf filename is ' + filename);
if (Ext.os.is.Android) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
}
else {
LRS.util.common.displayAjaxFailure(this.response, 1);
}
};
xhr.send(params)
;
My problem is that phonegap obviously doesn't understand 'new Blob' (logcat on Android shows 'Uncaught TypeError: Illegal constructor') and, as far as I can discover, it is in any case not possible to use phonegap's filewriter to write anything other than plain text (which I have done successfully elsewhere).
I would have thought there must be plenty of people who want their apps to download and save images, mp3s or whatever, so I'm hoping that some phonegap guru out there will be able to point me towards a solution. Please? Incidentally, my app's using a SQLlite database, so maybe a solution is to stick the binary data into there and display it from there? Trouble is I don't know how to display it.