Created
December 21, 2011 04:37
-
-
Save aaronksaunders/1504618 to your computer and use it in GitHub Desktop.
posting data with attachment to couchdb using iriscouch and appcelerator
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
// | |
// blog.clearlyinnovative.com | |
// Aaron K. Saunders | |
// Clearly Innovative Inc | |
// | |
var database_url = "http://greenlyyt.iriscouch.com/movie_store/"; | |
var user_name, password; | |
/** ---------------------------------------------------------------------------- | |
* | |
* ---------------------------------------------------------------------------- | |
*/ | |
var postAttachmentToServer = function(params, _success, _fail) { | |
var http = makeHttpClient(); | |
var f = Ti.Filesystem.getFile(params.filePath); | |
var contents = f.read(); | |
http.setRequestHeader('Content-Type', contents.mimeType); | |
var url = database_url + params.id + "/" + params.fileName + "?rev=" + params.rev; | |
http.open("PUT", url); | |
Ti.API.debug(url); | |
http.onload = function() { | |
Ti.API.debug(http.responseText); | |
var resp = JSON.parse(http.responseText); | |
if(_success) { | |
_success({ | |
"data" : params, | |
"attachment" : resp | |
}); | |
} | |
} | |
http.onerror = function(e) { | |
Ti.API.error("postAttachmentToServer " + JSON.stringify(e)); | |
if(_fail) { | |
_fail({ | |
"data" : params, | |
"error" : e | |
}); | |
} | |
} | |
http.send(contents); | |
Ti.API.debug(http.url); | |
} | |
/** | |
* | |
*/ | |
var postDataToServer = function(userData, _success, _fail) { | |
var http = Titanium.Network.createHTTPClient(); | |
http.setRequestHeader('Content-Type', 'multipart/form-data; charset=utf-8'); | |
var url = database_url + Ti.Platform.createUUID() + "new/"; | |
http.open("PUT", url); | |
Ti.API.debug(url); | |
http.onload = function() { | |
Ti.API.debug(http.responseText); | |
var params = JSON.parse(http.responseText); | |
if(userData.filePath) { | |
params.filePath = userData.filePath; | |
params.fileName = userData.fileName; | |
postAttachmentToServer(params,_success, _fail); | |
} else { | |
_success(params); | |
} | |
} | |
http.onerror = function(e) { | |
Ti.API.debug(e); | |
_fail(e); | |
} | |
http.send(JSON.stringify(userData)); | |
Ti.API.debug(http.url); | |
Ti.API.debug(userData.media); | |
} | |
var userData = { | |
"first" : "Aaron", | |
"last" : "Saunders", | |
"city" : "Washington", | |
"state" : "DC" | |
"fileName":"test_image.png" | |
"filePath":"test_image.png" | |
}; | |
postDataToServer(userData, | |
function(){ | |
alert('success'); | |
}, | |
function(){ | |
alert('fail'); | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment