Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download remote files async with Titanium
<html>
<head></head>
<body style="background-color:#1c1c1c;margin:0">
<div style="border-top:1px solid #404040">
<div style="color:#fff;;padding:10px">Welcome to Titanium</div>
</div>
<script type="text/javascript">
localFile = Titanium.Filesystem.getDesktopDirectory().toString() +
Titanium.Filesystem.getSeparator() +
'index.html';
remoteFile = 'http://www.codeboxed.com';
var httpClient = Titanium.Network.createHTTPClient();
httpClient.setTimeout(10000);
// Data received
httpClient.onload = function(e) {
// Create a filestream and write the received data to a file
var writeFile = Titanium.Filesystem.getFile(localFile);
var writeStream = Titanium.Filesystem.getFileStream(writeFile);
writeStream.open(Titanium.Filesystem.MODE_WRITE);
writeStream.write(this.responseData);
writeStream.close();
};
// Handle state change
httpClient.onreadystatechange = function(e) {
if (e.readyState == 4) {
// Download finished
}
};
// Handle error event
httpClient.onerror = function(e) {
console.log('HTTP error!');
};
httpClient.open('GET', remoteFile);
httpClient.send();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.