Download a file from a server with Adobe AIR


Sorry, this entry is only available in German. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

We’re just adding the feature to upload and download documents to taskmind, and there I came across various problems that I thought they’re worth a blog entry.

First I thought that it should be possible to use the same code in Flex and AIR but the opposite is the case. If you pass URLVariables to a URLRequest, then the flash player works correctly, the AIR runtime not. I’ve committed a bugreport to Adobe’s JIRA for that, have a look there for details. Fortunately there is a workaround for that.

The next one is that if you open the file browse dialog (e.g. by calling then you can pass a file name to the dialog but if the user changes that filename, then the extension is lost and the file is written without extension to the file system. This bug was already entered as FP-1435 to Adobe’s JIRA but Adobe doesn’t see that as bug (although there is no workaround in Flex for it and it’s really annoying for users), so they closed that bug and opened a new feature request (FP-2014). Let’s see if and how fast they’re gonna fix it…

Fortunately, in AIR we can come across that problem and with the code on another blog entry as basis, I’ve then written some code that allows you to download files with AIR and bypassing all those problems:

var fileName:String = "myFileName.txt";    // usually comes from a webservice
var extension:String = "txt";   // could also be calculated
var serverUrl:String = "http://localhost/storage?fileid=4443"; // also from service

var request:URLRequest = new URLRequest(serverUrl);
var stream:URLStream = new URLStream();
var file:File = File.documentsDirectory.resolvePath(fileName);

file.browseForSave("Download " + fileName);
file.addEventListener(Event.SELECT, function(...args):void {
    if (file.extension != extension) {
        // fix for see FP-2014 in Adobe's JIRA: if the user has changed the
        // filename then the extension is null, so we create a new File
        // instance and append the original extension again so that
        // this is preserved
        file = new File(file.nativePath + "." + extension);
    stream.addEventListener(Event.COMPLETE, function(evt:Event):void {
        var fileData:ByteArray = new ByteArray();
	stream.readBytes(fileData, 0, stream.bytesAvailable);
	var fileStream:FileStream = new FileStream();, FileMode.WRITE);
	fileStream.writeBytes(fileData, 0, fileData.length);
    // add other event listeners for PROGRESS, ERROR, and so on...


Previous Post
12. Wispri: WWW – Warum Was Wie
Next Post
13. Wispri: Gemeinsame Softwareentwicklung im Team trotz unterschiedlicher IDEs

Related Posts

AsFlavor: Structure101g for Flex and Actionscript

Together with “The Structure Guys” from Headway Software we developed an Actionscript “flavor” for their latest Structure101 product family member Structure101g(eneric). AsFlavor makes the same esteemed structural and architectural analysis functionality available to the Flex and Actionscript community, that is well known to Java and C++ developers since years.

Read More

FlexUnit 4 und TeamCity

We switched from FlexUnit to funit which seemed much more lightweight and came with out-of-the-box TeamCity support. Then FlexUnit 4 came up and IntelliJ IDEA 9 was released including a built-in FlexUnit runner. That alone would have been reason enough to switch over again, but additionally FlexUnit 4 seems to be a huge improvement over the 0.9 predecessor.

Read More

2 Comments. Leave new

Hey, great post- was looking for a simple way to run multiple files. Thanks for sharing!


This is what I’m doing. It’s not ideal – the problem being that it’s in the event handler, after the save has already been comitted. If the user inadvertently enters a filename that already exists there is no automatic popup requesting whether the user wants to replace a file. One would have to implement that check oneself.

var newFile:File = as File;
var fileType:String = newFile.type;
trace(“fileType = ” + fileType);
if ((fileType == null) || ( fileType != “.xml”))
newFile = new File( newFile.nativePath += “.xml” );


Leave a Reply to Steve Cancel reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed