2012年4月12日 星期四

Titanium: Image file download via Http

var downloadCoverImage = function(imageUrl){
var imageFileName = 'temp.png';
var fileRepository = Ti.Filesystem.applicationDataDirectory+'../Library/Caches/';
var destFile = fileRepository+imageFileName;  
var f = Titanium.Filesystem.getFile(destFile);
if(f.exists()){
f.deleteFile();
}
var c = Titanium.Network.createHTTPClient();
c.setTimeout(60000);
c.onload = function(e)
{
if( this.status==200 ){
f.write(this.responseData);
}
};
c.ondatastream = function(e)
{
Ti.API.info('Saving image - PROGRESS: ' + e.progress);
};
c.onerror = function(e)
{
Ti.API.info('error:'+e.error);
var errorDialog = Ti.UI.createAlertDialog({title:'錯誤', message:'無法連接伺服器,請檢查網絡連線'});
errorDialog.show();
errorDialog.addEventListener('click', function(e){
if(c) c.abort();
win.close();
});

};
c.open('GET',imageUrl);
c.send();
};

沒有留言:

張貼留言