Forked from deyvin/get_remote_file_titanium_mobile.js
Last active
December 16, 2015 10:49
-
-
Save bennett/5423231 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_remote_file(filename, url, fn_end, fn_progress ) { | |
Ti.API.info("[filename]" + filename); | |
Ti.API.info("[url]" + url); | |
var file_obj = {file:filename, url:url, path: null}; | |
file_obj.fresh = 0; // Assume the old file will be returned | |
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename); | |
if ( !file.exists() ) { | |
file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator; | |
//fn_end(file_obj); | |
Ti.API.info("arquivo existe"); | |
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename); | |
f.write(this.responseData); | |
var fcontent = f.read(); | |
Ti.API.info('[CONTENT] = ' + fcontent.text); | |
} | |
else { | |
if ( Titanium.Network.online ) { | |
var c = Titanium.Network.createHTTPClient(); | |
c.setTimeout(10000); | |
c.onload = function() | |
{ | |
if (c.status == 200 ) { | |
Ti.API.info("Ok"); | |
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename); | |
f.write(this.responseData); | |
var fcontent = f.read(); | |
Ti.API.info('[CONTENT] = ' + fcontent.text); | |
file_obj.path = Titanium.Filesystem.applicationDataDirectory + Titanium.Filesystem.separator; | |
file_obj.fresh = 1; // the file is new | |
} | |
else { | |
file_obj.error = 'file not found'; // to set some errors codes | |
Ti.API.info("error"); | |
} | |
// fn_end(file_obj); | |
}; | |
c.ondatastream = function(e) | |
{ | |
if ( fn_progress ) fn_progress(e.progress); | |
}; | |
c.error = function(e) | |
{ | |
file_obj.error = e.error; | |
// fn_end(file_obj); | |
}; | |
c.open('GET',url); | |
c.send(); | |
} | |
else { | |
file_obj.error = 'no internet'; | |
//fn_end(file_obj); | |
} | |
} | |
}; | |
//usage: | |
//get_remote_file("filename.txt", "http://yoururl.com/filename.txt", callback_to_end(), callback_to_progress()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment