简体   繁体   中英

Flex Air Mobile File Download

I am building an Adobe Air Mobile application currently targeting Android (and eventually iOS). I'm using Flash Builder 4.6 (which is Flex).

I have a requirement to download a zip file from a url, save it to local storage and unzip the contents.

There seem to be a few example on how to do a download in Flex/Air, but not many in Flex/Air/Mobile. Additionally, the mix of examples seem to be all over the place with namespace versions, etc.

Could someone please provide a succinct example on how to do this in an Air Mobile app?

Thank you!

Edit: What I've tried:

So, from the examples on the web, the first thing I did was add namespace in Flex declarations: xmlns:net="flash.net.*"

Then I added the following component: <net:URLLoader id="urlLoader" />

Then, in the button click event, I called it: urlLoader.load(new URLRequest(downloadUrl));

I get a run time error: Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: http:// Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: http:// Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: http:// ...

The problem is I don't know where to go from here since I'm not sure what's missing.

this code works for me on IOS:

public function download():void{
        var loader:URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.BINARY;
        loader.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
            Alert.show('error IO');
        });
        loader.addEventListener(Event.COMPLETE,downloadComplete);
        loader.load(new URLRequest(YOUR_URL));          
    }

private function downloadComplete(event:Event):void{
        try{
            var file:File=File.applicationStorageDirectory.resolvePath(YOUR_LOCAL_PATH);
            var ba:ByteArray  = event.target.data as ByteArray;
            var fileStream:FileStream = new FileStream();
            fileStream.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
                Alert.show('error IO');
            });
            fileStream.open(file, FileMode.WRITE);  
            fileStream.writeBytes(ba);  
            fileStream.addEventListener(Event.CLOSE, fileClosed);  
            fileStream.close(); 
            Alert.show("File downloaded succesfully")   
        }
        catch(eeee){
            Alert.show("error")
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM