Created
March 5, 2011 17:27
-
-
Save codeboxed/856522 to your computer and use it in GitHub Desktop.
Download remote files async with Titanium
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
<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