0
votes

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

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

1
How about you start with what you've tried, explain why it doesn't work, and then perhaps we can help you diagnose. How does that sound? - JeffryHouser
Thanks, flextras! I've added an edit to show some of the things I've tried so far. - Ilya
Are you using the 4.6.0 SDK, the version of the builder IDE doesn't make as much of a difference as the actual SDK being used, verify this in your project properties by right clicking the project and going to properties, also the version of AIR would be useful as well. Also you can use Charles web debugging proxy (if you've seen my posts in the past it may seem I work for them but I don't), also add fault handlers help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/… - shaunhusain

1 Answers

2
votes

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")
        }
    }