Upload images to a server
Hi,
How do I upload images to my server and get back the results on the iPhone (Using Titanium). Please help!
7 Answers
-
Accepted Answer
There are several ways.
1) One upload image to server and use the webview to see the image on the iphone.
2) Use ajax to copy image from server to iphone then you can display it. This is code from kitchen sink.
var xhr = Titanium.Network.createHTTPClient(); xhr.onload = function() { var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'ti.png'); f.write(this.responseData); imageView.url = f.nativePath; }; // open the client xhr.open('GET','http://www.appcelerator.com/wp-content/uploads/2009/06/titanium_desk.png'); // send the data xhr.send();
-
I thought you were uploading images to the server then trying to put them on the iphone. If you are going the other way then here is what you need to do.
Send images from iphone to server.
f1 = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,<image name>); i1 = f1.read(); xhr = Titanium.Network.createHTTPClient(); xhr.open('POST','<url to call>', false); // false makes it synchronous xhr.onload = function() { handleAfterSentRouting(this.responseText); }; xhr.send({media1: i1}); // media1 is the field the file information is in when you upload
So the above code will send a file from your phone to the server. The code I gave you before will get a file from your server to the phone.
Now I think you are covered in both ways. Hope this helps.
-
Hi,
Thanks for the reply! All done, but Im not sure if my php is correct. Have pasted my PHP code here. The images are not getting uploaded once I send it from the iPhone.<?php $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } ?>
But this works if I upload images from HTML page.
-
Hi,
Thanks a lot! And any idea about how the PHP should be on the server?
Have posted a detailed query here: http://bit.ly/bI96i1 -
Hi,
Thanks a lot! And any idea about how the PHP should be on the server?
Have posted a detailed query here: http://bit.ly/bI96i1 -
Hey Karthik,
See my tweet to you directly, but a Google search for "file upload PHP" turns up several examples of the server-side code you will need. The client side code in Titanium should be the same.
-Kevin
-
Hello there,
I know it's been about 4 years, but has ANYONE, figured out how to send images to a remote server? I have tried so many times, so many different ways, but still nothing. Anyone?
Thaks