Downloading image from server gets stuck

You must Login before you can answer or comment on any questions.

I am downloading images from server. On fn_end process the downloading gets stuck and c.setTimeout(10000); also not working.My titanium sdk is 2.0.1 and android version is 2.2. My code snippet as:

var imageCounter = 0;
 
function get_remote_file(filename, url, fn_end, fn_progress ) {
 
    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);
    }
    else {
        if ( Titanium.Network.online ) {
 
 
            var c = Titanium.Network.createHTTPClient();
 
            c.setTimeout(10000);
            c.onload = function()
            {
 
                if (c.status == 200 ) {
 
 
                    var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
                    f.write(this.responseData);
                    file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator;
 
                }else{
 
                    file_obj.error = 'file not found'; // to set some errors codes
                }
                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);
        }
 
    
    }
 
 
};
 
 
 
function fn_end1(fileObj){
 
Ti.API.info('file:'+fileObj.file);
Ti.API.info('url:'+fileObj.url);
Ti.API.info('path:'+fileObj.path);
Ti.API.info('error:'+fileObj.error);
 
imageCounter = imageCounter-1;
 
Ti.API.error('imageCounter:'+imageCounter);
 
Ti.API.error('imageCounter zero:'+imageCounter);
 
}
 
function fn_progress1(progress){
 
Ti.API.error('progress:'+progress);
 
}
 
get_remote_file(fileName,urlPath,fn_end1,fn_progress1);
Is there any better solution to download images from server and save it to application directory. Any suggestion or help. thanks in advance.

1 Answer

Your Answer

Think you can help? Login to answer this question!