1. #1
    Sencha User
    Join Date
    Jun 2012
    Posts
    129
    Vote Rating
    0
    Answers
    7
    munder is on a distinguished road

      0  

    Default Unanswered: How to save a blob with phonegap

    Unanswered: How to save a blob with phonegap


    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.

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,599
    Vote Rating
    434
    Answers
    3102
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    I'm not seeing anything with Sencha Touch involved here
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Jun 2012
    Posts
    129
    Vote Rating
    0
    Answers
    7
    munder is on a distinguished road

      0  

    Default


    Well, you're right, Mitchell, except that the rest of the app uses ST. There would appear to be many here using ST with phonegap, so I thought this would be the right place to ask. My apologies if I've chosen the wrong forum.