Download a file from a server with Adobe AIR

2 Kommentare

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...


Vorheriger Beitrag
12. Wispri: WWW – Warum Was Wie
Nächster Beitrag
13. Wispri: Gemeinsame Softwareentwicklung im Team trotz unterschiedlicher IDEs

Related Posts

Resourcebundles mit Ant automatisch auf Konsistenz prüfen

, ,
2 Kommentare

ResourceBundles werden in verschiedenen Programmiersprachen (Java, Adobe ActionScript,…) benutzt, um Anwendungen zu internationalisieren. Man erstellt für jede Sprache, in der die Anwendung später übersetzt werden soll eine Datei, in der dann die übersetzten Zeichenketten in der jeweiligen Sprache eingetragen sind. Dieser Blogeintrag zeigt, wie man ResourceBundles mit ANT automatisch Überprüfen kann.


20. Wissensspritze: Leistungsstarke UIs

pureMVC ist ein Framework zur Umsetzung des Model – View – Controller Architekturmusters, mit dem sich leistungsstarke RIA Applikationen entwickeln lassen.
Die Referenzimplementierung ist in Actionscript, pureMVC ist mittlerweile jedoch in sehr vielen Programmiersprachen verfügbar.


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.


2 Kommentare. Hinterlasse eine Antwort

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” );


Schreibe einen Kommentar zu Carl Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Bitte füllen Sie dieses Feld aus
Bitte füllen Sie dieses Feld aus
Bitte gib eine gültige E-Mail-Adresse ein.
Sie müssen den Bedingungen zustimmen, um fortzufahren