Skip to content

Instantly share code, notes, and snippets.

@codeboxed
Created March 5, 2011 17:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeboxed/856522 to your computer and use it in GitHub Desktop.
Save codeboxed/856522 to your computer and use it in GitHub Desktop.
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