function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EchoEchoEchoEcho 

Using XMLHttpRequest2 in iOS 5 or Android OS 3 to download binary files using HTML5/Phonegap

Posted this the other day on how to download images from the Chatter API using XMLHttpRequest2, which is available on iOS 5 and Android OS 3.0:

 

http://www.modelmetrics.com/tomgersic/using-xmlhttprequest2-in-ios-5-to-download-binary-files-using-html5phonegap/

 

If you wanted to modify forcetk to do this easily, here's the code:

 

    /**
     * Utility function to query the Chatter API and download a file
     * @param path resource path relative to /services/data
     * @param mimetype of the file
     * @param callback function to which response will be passed
     * @param [error=null] function to which request will be passed in case of error
     * @param rety true if we've already tried refresh token flow once
     **/
    forcetk.Client.prototype.getChatterFile = function(path,mimeType,callback,error,retry) {
        var that = this;
    
        var url = this.instanceUrl + path;

        var request = new XMLHttpRequest();
        
                
        request.open("GET", url, true);
        request.responseType = "arraybuffer";
        
        request.setRequestHeader(that.authzHeader, "OAuth " + that.sessionId);
        request.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion);
        
        request.onreadystatechange = function() {
            // continue if the process is completed
            if (request.readyState == 4) {
                // continue only if HTTP status is "OK"
                if (request.status == 200) {
                    try {
                        // retrieve the response
                        callback(request.response);
                    }
                    catch(e) {
                        // display error message
                        alert("Error reading the response: " + e.toString());
                    }
                }
                //refresh token in 401
                else if(request.status == 401 && !retry) {
                    that.refreshAccessToken(function(oauthResponse) {
                        that.setSessionToken(oauthResponse.access_token, null,oauthResponse.instance_url);
                        that.getChatterFile(path, mimeType, callback, error, true);
                    },
                    error);
                } 
                else {
                    // display status message
                    error(request,request.statusText,request.response);
                }
            }            
            
        }

        request.send();
        
    }

 

cloudcodercloudcoder

Thanks. You should certainly create a pull request on the github repo and collaborate.

https://github.com/developerforce