The next hurdle ive come across while working on Air. I needed to save files from a remote site, but found there was a lack of documentation or I just hadn’t looked hard enough ofcourse. I thought i would provide an example and please note I use AJAX/HTML for Air development.

// Assign variable the link to the remote file.
var url = "http://<URL to file you want to download>";
// Specify the name you want to save the file as.
var fileName = "my_file_name.txt";
// HTTP Request to the specified URL .
var url = new air.URLRequest(url);
// Assigns a new URL loader.
var loader = new air.URLLoader();
// Tells the loader to use Binary as the data format.
loader.dataFormat = air.URLLoaderDataFormat.BINARY;
// Adds a listener to run when the download completes, runs downloadCompleteHandler function.
loader.addEventListener(air.Event.COMPLETE, downloadCompleteHandler);
// Loads the actual data the was requested above.
// Function to be run when the load completes
function downloadCompleteHandler(e) {
	// Put loaded data into ByteArray
	var data = air.ByteArray(;
	// Specifys a new directory in the Application Storage Dir
	var dir = air.File.applicationStorageDirectory.resolvePath("Example Folder");
	// Actually creates the directory specifed.
	// Assign path to Application Storage Directory.
	var file = air.File.applicationStorageDirectory;
	// Specify our newly created folder within the App Storage Directory.
	file = file.resolvePath("Example Folder");
	// Specify what we want our downloaded file to be called. we assigned the file name above.
	file = file.resolvePath(fileName);
	// Create new File Stream used to write to newley create file within the App Storage Dir
	var fileStream = new air.FileStream();
	// Open the new file in Update mode (means we can read/write to the new file), air.FileMode.UPDATE);
	// Writes the loaded data from the ByteArray to our new file, the 0 and data.length tell it to write all of the bytes captured by the byte array.
	fileStream.writeBytes(data, 0, data.length);

There you have it, you should now be able to download and write remote files in Adobe Air! Ive tried to explain best I can but let me know if ive made any mistakes and ill alter it. You can also go further with this example and add more event listeners  to capture errors or the progress of the download.

Alex Smirnov

Senior Java Developer. JAVA, PHP, Cold Fusion, SQL, Air, Flash.

More Posts

Tagged with: