Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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!

— asked March 28th 2010 by Karthik k
  • images
  • iphone
0 Comments

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();
    
    — answered March 28th 2010 by General Usage
    permalink
    0 Comments
  • 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.

    — answered March 28th 2010 by General Usage
    permalink
    2 Comments
    • Can I upload image by using GET instead of POST?

      — commented February 13th 2012 by Gaurav Sood
    • No you can't

      — commented May 24th 2013 by Eric Wooley
  • 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.

    — answered March 28th 2010 by Karthik k
    permalink
    0 Comments
  • 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

    — answered March 28th 2010 by Karthik k
    permalink
    0 Comments
  • 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

    — answered March 28th 2010 by Karthik k
    permalink
    0 Comments
  • 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

    — answered March 29th 2010 by Kevin Whinnery
    permalink
    0 Comments
  • 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

    — answered December 14th 2013 by Paulo Dichone
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.