Skip to content

Instantly share code, notes, and snippets.

@furi2
Created November 19, 2011 07:36
Show Gist options
  • Save furi2/1378595 to your computer and use it in GitHub Desktop.
Save furi2/1378595 to your computer and use it in GitHub Desktop.
Uploading multipart/form-data type data from Titanium
var content = '';
var boundary = '---------------------------170062046428149';
content += '--'+ boundary + '\r\n';
content += 'Content-Disposition: form-data; name="uploadToken"\r\n';
content += '\r\n';
content += upload_token + '\r\n';
content += '--'+ boundary + '\r\n';
content += 'Content-Disposition: form-data; name="destFolderPath"\r\n';
content += '\r\n';
content += '/Images\r\n';
content += '--'+ boundary + '\r\n';
content += 'Content-Disposition: form-data; name="fileContent"; filename="test.jpg"\r\n';
content += 'Content-Type: binary/octet-stream\r\n';
content += '\r\n';
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'test.jpg');
var full_content = Ti.createBuffer({value: content});
var fileStream = Ti.Stream.createStream({ source : file.read(), mode : Titanium.Stream.MODE_READ});
var content_size = full_content.append(Ti.Stream.readAll(fileStream));
Ti.API.debug('Appended File Size : ' + content_size ); // ==> same as file.size
content = '\r\n'
content += '--'+ boundary + '--\r\n';
full_content.append(Ti.createBuffer({value : content}));
xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
var send_data = full_content.toBlob();
Ti.API.debug(send_data);
// xhr.send(send_data); // ==> According to the server response, it sends more bytes than the original file size. (original 105kb, sent data 180kb)
// xhr.send({image : send_data }); // ==> Content length becomes 0
// xhr.send({image : file.read() }); // ==> Content length becomes 0
@chrisribe
Copy link

Has anyone progressed on this. I got my code to work on iOS but NOT on android.... WTF !?

@SquirrelMobile
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment