konstantinbueschel
12/11/2016 - 5:46 PM

Appcelerator uploading image with Parse REST API - From https://parse.com/questions/appcelerator-titanium-uploading-image-with-rest-api

Appcelerator uploading image with Parse REST API - From https://parse.com/questions/appcelerator-titanium-uploading-image-with-rest-api

var FileModel = function() {
    this.saveImage = function($blob, $callback, $error) {
        console.log("saveImage", $blob.mimeType);

        var request = Titanium.Network.createHTTPClient({
            onload: function(e) {
                $callback(JSON.parse(this.responseText));
            },
            onerror: function(e) {
                $error(e.message);
            }
        });

        if ($blob.mimeType == "image/jpeg" || $blob.mimeType == "image/bitmap") {
            request.open('POST', 'https://api.parse.com/1/files/pic.jpg', true);
        } else if ($blob.mimeType == "image/png") {
            request.open('POST', 'https://api.parse.com/1/files/pic.png', true);
        }
        request.setRequestHeader('X-Parse-Application-Id', "Your App ID");
        request.setRequestHeader('X-Parse-REST-API-Key', "Your REST API KEY");
        request.setRequestHeader('X-Parse-Session-Token', Parse.User.current()._sessionToken);
        request.setRequestHeader('Content-Type', $blob.mimeType);
        request.send($blob);
    }
}

module.exports = new FileModel();


//Use like

var File = require("FileModel");
File.saveImage(someBlob, 
    function(results) {
        //Success
        var name = results.name;
        var url = results.url
    },
    function(error) {
        //Oops, Something went wrong
    }
);