I am trying to upload video file from android emulator, Genymotion and Mobile Device in Titanium Appcelerator.
I am getting success when I upload small files capturing from emulator around about 200-300kb is working fine. its uploading and getting response from server.
When I am trying to upload file around 3-5MB then I am not getting response from server. Some times I am getting response too late after 2-4 minutes and file was get uploaded at that time. But frequently its not uploaded and not getting response anymore.
I tried different code posted on web but did not get luck 100%.
In the log for on data stream I can see process 0-1 but for big file its stopped at .9999x, it is not reaching at 1.
[INFO] : ONSENDSTREAM - PROGRESS: 0.9998882582315288
[INFO] : ONSENDSTREAM - PROGRESS: 0.9999253846335128
[INFO] : ONSENDSTREAM - PROGRESS: 0.9999625110354967
[INFO] : ONSENDSTREAM - PROGRESS: 0.9999996374374807
here is my sample code for testing.
Titanium Button Click
var intent = Titanium.Android.createIntent({
action : Ti.Android.ACTION_PICK,
type : "video/*"
});
intent.addCategory(Ti.Android.CATEGORY_DEFAULT);
$.index.activity.startActivityForResult(intent, function(ei) {
if (ei.error) {
alert("Error");
} else {
var dataUri = ei.intent.data;
var xhr = Titanium.Network.createHTTPClient(/*{enableKeepAlive:false}*/);
xhr.setTimeout(1000 * 60 * 5);
xhr.open('POST', 'http://gaurangchhatbar.in/ws/apitest/videouploadcode.php');
xhr.setRequestHeader("enctype", "multipart/form-data");
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.onerror = function(ex) {
alert(ex.error);
};
xhr.onload = function() {
alert(this.responseText);
};
xhr.onsendstream = function(ex) {
Ti.API.info('ONSENDSTREAM - PROGRESS: ' + ex.progress);
};
var filename = "UploadedFile-" + (new Date()).toString();
filename = filename.replace(/\./g,"-");
filename = filename.replace(/\:/g,"-") + ".mp4";
Ti.API.info(filename);
var source = Ti.Filesystem.getFile(dataUri);
var fileData = Ti.Filesystem.getFile(Ti.Filesystem.getApplicationDataDirectory(), filename);
source.copy(fileData.nativePath);
if (fileData.exists()) {
var fileContent = fileData.read();
if (fileContent)
xhr.send({
video_path : fileContent,
action : "uploadvideo"
});
else
alert('Did not get any data back from file content');
} else
alert('Did not get a file data for : ' + dataUri);
}
});
PHP Code
$target_path = "video/";
$target_path = $target_path . basename( $_FILES['video_path']['name']);
if(move_uploaded_file($_FILES['video_path']['tmp_name'], $target_path)) {
return "success";
} else{
return "falied!";
}
I tried native android application to upload file from link : Android Native App for Upload File to Server
It is working great.
Can any one know why response taking too much time? or let me know if I forgot anything.
I am working on
Appcelerator Studio, build: 4.5.0.201602170821 Titanium SDK 5.2.2.GA Alloy Project Mac OS X El Capitan Version 10.11.4
Sadly, the onsendstream() progress on android is just wrong (way too fast). I've reported this already as a bug here:
https://jira.appcelerator.org/browse/TIMOB-20483
Could it be that you are just confused by the sendstream callback and your code is all fine?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With