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";
}
?>
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…
-
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
-
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.