Created
November 19, 2011 07:36
-
-
Save furi2/1378595 to your computer and use it in GitHub Desktop.
Uploading multipart/form-data type data from Titanium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Has anyone progressed on this. I got my code to work on iOS but NOT on android.... WTF !?
@chrisribe yes, use this code : https://gist.github.com/SquirrelMobile/014a5de3f5199799da3d0fce2f5d90df
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Guys, I know this is from a long time ago, but I am very desperate at this point. I am trying to send an image to my server and it's not working. Below is my code:
and here's my php server-side script:
Any help, guidance you can send my way is highly appreciated. I have been trying to get this to work for about 2 weeks now, and no luck. Thank you very much for your help in advance.
Regards