Titanium Community Questions & Answer Archive

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

Huge Limitation in Titanium? - xhr File Upload over 3G - (FAO Appcelerator)

Firstly i find this Q&A forum extremely helpful and have got some fantastic help and feedback in the past but I have been struggling to get an xhr file upload to work over a 3g connection for the last week and a half now and i can only come to the conclusion that it is down to a Titanium issue rather than my code. No matter what code i implement i never get the image sent over 3G, yet WIFI works perfectly.

I know there has been plenty of posts on the matter here (listed at end of post) with the majority still without a solution. What shows that it looks like a Titanium error rather than a developers is that even the Kitchen Sink Demo and the Wiki guide (found here:http://wiki.appcelerator.org/display/guides/File+Uploads+and+Downloads) have the same issue when tested on device.

In the wiki guide, Appcelerator even state that 'A common need in a mobile application is to upload a file (like an image) to a remote server'. Yet their supplied reference code fails over 3G

I have tested from the Titanium 1.7.5 SDK, ioS SDK 4-5, and on an iPhone 3GS and iPhone 4S as well without a difference.

Apologies for the slight rant but i'm amazed that no one from Appcelerator has taken up the issue within this forum and resolved the problem, especially updating the sample code in the docs. I love everything else about Titanium but without being able to solve this i will have to look into alternatives such as Objective C or even Phonegap which uploads over 3G fine.

I created a full topic on the matter around a week ago here:
http://developer.appcelerator.com/question/128361/image-upload-over-3g-failure—how-to-solve

and a search through the Q&A's show so many of the same problem:
http://developer.appcelerator.com/question/124407/help-with-httpclient-timeout-on-edge-connection
http://developer.appcelerator.com/question/127561/xhr-upload-doent-work-on-3g—not-timeout-issue
http://developer.appcelerator.com/question/128273/problem-loading-data-over-3g-not-working-but-works-over-wifi
http://developer.appcelerator.com/question/28491/3g–edge-problem-with-httpclient-always-works-with-wi-fi
http://developer.appcelerator.com/question/119408/image-upload-constantly-failing-over-3g
http://developer.appcelerator.com/question/118833/uploading-images-over-3g-fails
http://developer.appcelerator.com/question/95661/image-upload-image-files-null-over-3g-works-fine-over-wifi
(and so on…)

My Full latest code which has taken on board all the suggestion on here

function upload(media_file){

            // Check if valid internet connection 
            //if (Titanium.Network.online == false) {Titanium.UI.createAlertDialog({title:'OFFLINE', message:'No Network Connection Detected.'}).show(); return;}   

            // Set Timer Disabled Twice - stop the phone going to sleep during an upload
            Ti.App.idleTimerDisabled = false; Ti.App.idleTimerDisabled = true;

            var xhr = Titanium.Network.createHTTPClient({

            onload: function(e) {
                Ti.API.info('DONE! - IN ONLOAD ' + this.status + ' readyState ' + this.readyState);
                Ti.App.idleTimerDisabled = false;
                var alrt_Window = Titanium.UI.createAlertDialog({
                title: 'DONE! - IN ONLOAD',
                message: 'this.status = ' + this.status + ' this.responseText ' + this.responseText + ' e.error ' + e.error,
                buttonNames: ['OK'],
                });            
            alrt_Window.show();

            },// end onload

            onerror: function(e){
                Ti.API.info('IN ERROR ' + e.error);

                var alrt_Window = Titanium.UI.createAlertDialog({
                title: 'onerror ERROR!',
                message: 'this.status = ' + this.status + ' this.responseText ' + this.responseText + ' e.error ' + e.error,
                buttonNames: ['OK'],
                });
            alrt_Window.show();

            },// end onerror

            onsendstream: function(e){
                Ti.API.info('in onsendstream : progress:'+e.progress+' - : status '+this.status+'  readystate:'+this.readyState);
            },// end onsendstream

            enableKeepAlive:false,

            });

            xhr.setTimeout(60000); // in milliseconds

            var endPoint = 'http://www.xxxxx.com/testing/titanium_upload.php';

            xhr.open('POST',endPoint);

            xhr.send({server_filename:S.app.itemNumber, file:media_file});       

        }; // end function onload

PHP:

<?php

if(!empty($_FILES)){

    $theFileName = $_FILES['file']['name'];

    $temp_filename = $_POST['filename'];

    function getExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }
            $l = strlen($str) - $i;
            $ext = substr($str,$i+1,$l);
            return $ext;
    }

    $extension = getExtension($theFileName);
    $extension = strtolower($extension);
    $new_filename = $temp_filename.'.'.$extension;

    $uploadDestinationPath = 'uploads/';

    if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadDestinationPath."/".$new_filename)){
        echo "$_FILES Set and ".$_POST['filename']." Uploaded";
    }
    else {
        echo "$_FILES Set but Varibles Undefined";
    }
}
else {
    echo "$_FILES Not Set";
}
?>
— asked November 24th 2011 by Jon Odell
  • 3g
  • titanium
  • upload
  • xhr
1 Comment
  • Hey Jon, Thanks for your great post. I'm having the same problem. Have you happened to try an objective-c version? I think that will be the next step for me. I'll write some objective-c code and see if I have the same problem. If that works at least I'll have a work around until Appcelerator can look at the issue.

    — commented December 7th 2011 by Jeremy Swensen

3 Answers

  • Can anyone from Appcelerator respond to this? I can't believe so many people have flagged this issue up yet nothing is being done about it…

    — answered November 28th 2011 by Jon Odell
    permalink
    0 Comments
  • Jon - I found some more interesting information regarding using 3G on iphone.

    Using your iphone over 3G, visit http://www.whatismyip.com/ using safari. And then refresh it a few times and see if the ip address of your 3G connection changes. The problem someone else was having was with sessions. I know your not working with sessions (i think), but I still found it interesting.

    Issue found here:

    http://developer.appcelerator.com/question/121244/iphone-4-3g-and-ip-addresses

    — answered November 24th 2011 by Darren Adams
    permalink
    1 Comment
    • Thanks Darren. On Vodafone in seems like the IP address is fine, only on certain networks. Thanks for the link though

      — commented November 28th 2011 by Jon Odell
  • I also had problems with file upload via 3G or Edge, but somehow it works now. Just uploaded a large movie successfully.

    In my php script I have also:

    ignore_user_abort(true);
    set_time_limit(0);
    

    I am not using Apache, but nginx, but I think this should make no difference.

    — answered December 2nd 2011 by Danny Pham
    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.