I've got this code in my view:
Ti.Media.showCamera({ success : function(event) { var new_photo = event.media; Ti.Media.saveToPhotoGallery(new_photo); ImageUploadViewInit(new_photo); }, cancel : function() { Ti.API.info('camera cancelled'); }, error : function(error) { // set up an alert message var error_msg = Titanium.UI.createAlertDialog({ title : 'OH NO!' }); // Here is where it will detect the lack of a camera if (error.code == Titanium.Media.NO_CAMERA) { error_msg.setMessage('Your device does not have a camera!'); } else { error_msg.setMessage('Unexpected error: ' + error.code); } error_msg.show(); }, // set to allow pinch zoom square photo edit option or not allowEditing : false, // force still photos only mediaTypes : Titanium.Media.MEDIA_TYPE_PHOTO });It displays the thumbnail on the image that was taken (with the camera) in the top left corner along with a couple of text fields for the user to fill in. What I want to do is have the user press upload, and it uploads everything to a .NET server. My upload code is this (take from the tutorial here http://www.smokycogs.com/blog/titanium-tutorial-how-to-upload-a-file-to-a-server/):
function uploadImage(obj){ var xhr = Titanium.Network.createHTTPClient(); var boundary = '----'+Math.floor((Math.random()*9999999999)+1); var header = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"userid\"\r\n\r\n" + Ti.App.userInfo.Id + "\r\n"; header += "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"userhash\"\r\n\r\n" + Ti.App.userInfo.Hash + "\r\n"; header += "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"tags\"\r\n\r\n" + obj.tags + "\r\n"; header += "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"title\"\r\n\r\n" + obj.title + "\r\n"; header += "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"desc\"\r\n\r\n" + obj.desc + "\r\n"; header += "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"itemtypeid\"\r\n\r\n" + 1 + "\r\n"; header += "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"sourcesystemtagid\"\r\n\r\nKoopzMobile\r\n"; header += "--" + boundary + "\r\n"; header += "Content-Disposition: form-data; name=\"userfile\";"; header += "filename=\"" + obj.filename + "\"\r\n;"; header += "Content-Type: application/octet-stream\r\n\r\n"; var userDir = Titanium.Filesystem.getUserDirectory(); var uploadFile = Titanium.Filesystem.getFile(userDir, obj.filename); var uploadStream = Titanium.Filesystem.getFileStream(uploadFile); uploadStream.open(Titanium.Filesystem.FILESTREAM_MODE_READ); content = uploadStream.read(); uploadStream.close(); var fullContent = header + content + "\r\n--" + boundary + "--"; xhr.open("POST",{MY URL}); xhr.setRequestHeader("Content-type", "multipart/form-data; boundary=\"" + boundary + "\""); xhr.setRequestHeader("Connection", "close"); xhr.send(fullContent); xhr.onreadystatechange = function(){ if (this.readyState == 4){ alert(this.responseText); } } }I can't seem to get anything to work. I can't get the file path from the image, and it stops there.
Can anyone help? Am I at least on the right track?
1 Answer
Accepted Answer
I upload photos by just sending the "event.media" / BLOB to the server via POST...as smooth as that... sometimes i get errors with iphone photos bc they are huge in 4s and iphone 5 and my php server limits files by 2mb..so have that in mind too.
var data = { id: userID, content: message, type: type, foto: event.media }; var client = Ti.Network.createHTTPClient({ // function called when the response data is available onload : function(e) { Ti.API.info(this.responseText); }, // function called when an error occurs, including a timeout onerror : function(e) { Ti.API.debug(e.error); }, }); // Prepare the connection. client.open("POST", url); // Send the request. client.send(data);
Your Answer
Think you can help? Login to answer this question!