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

21. Wissensspritze: Actionscript Strukturanalyse mit Structure101g

Structure101 ist im Java Umfeld schon länger als mächtiges Werkzeug zur Sourcecode-Strukturalanyse und Überwachung bekannt. Mit Structure101g bietet Headwaysoftware eine generische Variante dieses Tools, die mittels geeigneten Plugins beliebige Daten analysieren kann. Diese Wissenspritze widmet sich Structure101g für Actionscript, das von Headwaysoftware und Catalysts gemainsam entwickelt wurde.


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.


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

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