Adobe AIR - How can I download files from web server without save dialog
Hi I'm Leo Hyun from South Korea.
So I want to make downl开发者_JAVA技巧oader application using AIR. My web server has somefiles. I want to download that files and save my desktop without save dialog. I don't know how can I fix it. actually I'm just beginner for AIR.
I'm using Flash Builder 4 & Flex 4.0 & AIR 2.0.
Somebody help me plz!
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="startApp();">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.net.FileReference;
private var fr:FileReference;
private var urlRequest:URLRequest;
private var downloadPDFFile:String="<!--MY WEB SERVER LOCATIONS-->/TEST.pdf";
private function startApp():void
{
fr = new FileReference();
urlRequest = new URLRequest();
}
private function DownloadFile():void
{
urlRequest.url = downloadPDFFile;
fr.download(urlRequest);
}
protected function btnDownload_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
DownloadFile();
}
]]>
</fx:Script>
<mx:Button verticalCenter="0" horizontalCenter="0" label="Download File ASD" id="btnDownload" click="btnDownload_clickHandler(event)"/>
</s:WindowedApplication>
Load the file contents using a URLLoader
or HTTPService
and write it to a file of your choice using the File and FileStream classes. Be careful not to overwrite user's own files.
//inside complete handler of the loader:
var file:File = File.documentsDirectory.resolvePath("test.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTF(str);
fileStream.close();
精彩评论