Created
August 13, 2013 08:35
-
-
Save rborn/6219080 to your computer and use it in GitHub Desktop.
Raw multi file download
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
var get_file = function(url, callback) { | |
var xhr = Ti.Network.createHTTPClient({ | |
onload: function(e) { | |
// this function is called when data is returned from the server and available for use | |
// this.responseText holds the raw text return of the message (used for text/JSON) | |
// this.responseXML holds any returned XML (including SOAP) | |
// this.responseData holds any returned binary data | |
console.log('downloaded '+ url); | |
callback({ | |
url: url, | |
data: this.responseData | |
}); | |
}, | |
onerror: function(e) { | |
// this function is called when an error occurs, including a timeout | |
console.log('error '+ url); | |
callback({ | |
url: url, | |
error: e.error | |
}); | |
}, | |
timeout: 5000 | |
/* in milliseconds */ | |
}); | |
xhr.open("GET", url); | |
xhr.send(); // request is actually sent with this statement | |
}; | |
var files_arr = [ | |
'http://farm6.staticflickr.com/5132/5502816868_67309ca2e6_o.jpg', | |
'http://farm5.staticflickr.com/4062/4398223914_8039585c48_o.jpg', | |
'http://farm6.staticflickr.com/5094/5416175148_3753a5cc90_o.jpg', | |
'http://farm9.staticflickr.com/8044/8124564455_66bf80d3b7_o.jpg', | |
'http://farm9.staticflickr.com/8301/7863005744_942b6c64d7_o.jpg', | |
'http://farm8.staticflickr.com/7300/9194232482_1cee5642e2_o.jpg' | |
]; | |
var current_download = 0; | |
var win = Ti.UI.createWindow({ | |
backgroundColor: '#fff' | |
}); | |
var pb = Titanium.UI.createProgressBar({ | |
top: 10, | |
width: 300, | |
min: 0, | |
max: 1, | |
value: 0, | |
color: '#f00', | |
message: 'Downloading '+current_download+' of '+ files_arr.length, | |
font: { | |
fontSize: 14, | |
fontWeight: 'bold' | |
}, | |
style: Titanium.UI.iPhone.ProgressBarStyle.BAR | |
}); | |
win.add(pb); | |
pb.show(); | |
var callback = function(){ | |
current_download++; | |
pb.value = current_download/files_arr.length; | |
pb.message = 'Downloading '+current_download+' of '+ files_arr.length; | |
if ( current_download < files_arr.length) { | |
get_file(files_arr[current_download], callback); | |
} | |
}; | |
get_file(files_arr[current_download], callback); | |
Ti.UI.backgroundColor = '#0f0'; | |
win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment