Created
March 27, 2012 13:45
-
-
Save deyvin/2216003 to your computer and use it in GitHub Desktop.
Get remote file with Titanium Mobile
This file contains hidden or 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}; | |
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; | |
} | |
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()) |
Can you please tell me how can I download pdf file getting in response.
I am getting pdf url and pdf name in responce.
I tried many ways but I cant download that pdf.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks so much for this. Took me a long time to find, but it works well.