Einfacher Download Manager
Dieses Tutorial zeigt die Erstellung eines einfachen Download Managers mithilfe von Adobe AIR. Mit diesem Tool können Sie eine beliebige Datei von einem Webserver auf Ihr lokales Dateisystem übertragen.
Diese Applikation funktioniert wie folgt: In das Textfeld geben Sie einen beliebige URL zu einer Datei auf einem Webserver an. In diesem Beispiel, habe ich die URL einer FLV Datei voreingestellt. Beim Klick auf den Button beginnt der Download Prozess und wird mittels eines Fortschritsbalkens (ProgressBar) angezeigt. Die jeweilige Datei wird auf Ihrem Desktop gespeichert. Ist der Download abgeschlossen, können Sie erneut eine Datei zum Download angeben.
Die Applikation verwendet die Klasse DownloadManager, welche dem Source Code beiliegt (siehe ganz unten!).
DownloadManager.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
<mx:Script>
<![CDATA[
import com.elad.framework.utils.DownloadManager;
import mx.controls.Alert;
private var DownloadDirectory:String;
private var FileName:String;
private function init():void
{
DownloadDirectory = File.desktopDirectory.nativePath;
}
private function downloadFile():void
{
DownloadBtn.enabled = false;
DownloadPath.enabled = false;
FileName = DownloadPath.text.substring(DownloadPath.text.lastIndexOf("/")+1, DownloadPath.text.length);
var downloadHelper:com.elad.framework.utils.DownloadManager = new com.elad.framework.utils.DownloadManager();
downloadHelper.addEventListener(ProgressEvent.PROGRESS, onDownloadProgress);
downloadHelper.addEventListener(Event.COMPLETE, onDownloadComplete);
downloadHelper.addEventListener(IOErrorEvent.IO_ERROR, onDownloadIOError);
downloadHelper.downloadFileFromServer(DownloadPath.text, DownloadDirectory + "/" + FileName);
}
private function onDownloadProgress(event:ProgressEvent):void
{
var value:Number = event.bytesLoaded;
var total:Number = event.bytesTotal;
var precent:Number = Math.round(value*100/total);
if (progressBar.minimum==0)
{
progressBar.minimum = value;
progressBar.maximum = total;
}
progressBar.label = "Download "+precent+"%";
progressBar.setProgress(value, total);
}
private function onDownloadComplete(event:Event):void
{
Alert.show("Download erfolgreich beendet!");
DownloadBtn.enabled = true;
DownloadPath.enabled = true;
}
private function onDownloadIOError(event:IOErrorEvent):void
{
Alert.show(event.text);
}
]]>
</mx:Script>
<mx:ProgressBar id="progressBar" x="10" y="40" minimum="0" label="Download 0%" mode="manual" width="250"/>
<mx:TextInput id="DownloadPath" x="10" y="10" width="250">
<mx:text>http://adobe.edgeboss.net/download/adobe/adobetv/gotoandlearn/airupdate.flv</mx:text>
</mx:TextInput>
<mx:Button id="DownloadBtn" x="268" y="10" label="Download" click="downloadFile()"/>
</mx:WindowedApplication>
Dieses Tutorial können Sie als Flex ZIP Datei herunterladen und direkt in FlexBuilder importieren.
Link: AIR DownloadManager Quellcode (ZIP Flex Import)


