Skip to content

Instantly share code, notes, and snippets.

@erinlin
Forked from furi2/gist:1378595
Created January 4, 2013 09:53
Show Gist options
  • Save erinlin/4451324 to your computer and use it in GitHub Desktop.
Save erinlin/4451324 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment