Skip to content

Instantly share code, notes, and snippets.

@yec
Created February 2, 2012 00:03
Show Gist options
  • Save yec/1720276 to your computer and use it in GitHub Desktop.
Save yec/1720276 to your computer and use it in GitHub Desktop.
sample function for file upload
Drupal.file = {
create : function(file_raw) {
var url = 'http://74.82.57.136/gsk/?q=api/file/create_raw';
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {
var file_object = JSON.parse(this.responseText);
Ti.API.log(file_object);
// Return a file info object. file_object[0].fid is the file id to reference
Ti.fireEvent('drupal:file:created', file_object[0]);
}
xhr.onerror = function() {
Ti.API.log(this.responseText);
Ti.API.log(this.status);
//Drupal.user.logout();
}
// file_raw is event.media
data = {'files[file_contents]' : file_raw};
xhr.open("POST", url);
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
xhr.setRequestHeader('Connection', 'close');
xhr.send(data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment