Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Upload Image To Webserver Titanium Android using PHP

$
0
0
PHP Code Of File : photoUploader.php' <?php //this function returns a random 5-char filename with the jpg extension function randomFileName() { $length = 5; $characters = 'abcdefghijklmnopqrstuvwxyz'; $string = ''; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters))]; } return $string . '.jpg'; } //create the random filename string and uploads target variables $randomString = randomFileName(); $target = 'upload/'; $target = $target . $randomString; if(move_uploaded_file($_FILES['media']['tmp_name'], $target)) { //output the location to our image echo 'http://www.servevername.com/app-data/upload/' . $randomString; } else { echo "false"; } ?> -------------------------------------------------------------------------Now Add Following Code In app.js-------------------------------------- var win = Ti.UI.createWindow({ backgroundColor : "#FFF" }); var fbButton= Titanium.UI.createButton({ title:'open gallery', width: 250, height: 48, top:400 }); function UploadPhotoToServer(media){ if (Titanium.Network.online == true) { var progressIndicator = Ti.UI.Android.createProgressIndicator({ message: 'Loading...', location: Ti.UI.Android.PROGRESS_INDICATOR_DIALOG, type: Ti.UI.Android.PROGRESS_INDICATOR_INDETERMINANT, cancelable: true, }); //progressIndicator.show(); var xhr = Titanium.Network.createHTTPClient(); xhr.onerror = function(e){ alert('IN ERROR ' + e.error); alert('Sorry, we could not upload your photo! Please try again.'); }; xhr.onload = function(){ alert('IN ONLOAD ' + this.status + ' readyState ' + this.readyState); }; xhr.onsendstream = function(e){ //alert('ONSENDSTREAM - PROGRESS: ' + e.progress); }; // open the client xhr.open('POST', 'http://www.servevername.com/app-data/photoUploader.php'); //the server location comes from the 'strings.xml' file // send the data xhr.send({ media: media, }); } else { alert('You must have a valid Internet connection in order to upload this photo.'); } } fbButton.addEventListener('click',function(e) { Titanium.Media.openPhotoGallery({ success:function(event) { alert('Our type was: '+event.mediaType); if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) { UploadPhotoToServer(event.media); } }, cancel:function() { }, error:function(err) { Ti.API.error(err); }, mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO] }); }); win.add(fbButton); win.open();

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>