Simone Accoroni
Simone Accoroni

Reputation: 11

Adobe air on iOS - FileReference Download Error

When I use the Download method of the FileReference class, everything works fine on Desktop and Android, but I get an error on iOS.

This is the code:

var req = new URLRequest(url);
var localRef:FileReference = new FileReference();
localRef.download(req);

On iOS I'm getting an Alert:

Download Error
File downloads not supported.

I already tried to NavigateToUrl() and it asks save the file in Dropbox or another App.

How can I fix this error?

Upvotes: 1

Views: 380

Answers (1)

Josh
Josh

Reputation: 8149

You shouldn't use FileReference on mobile (or AIR, in general, though it does open the Load/Save dialog on desktop so there can be some use there). You should instead use File and FileStream, which give you far more control over the file system.

In this case, you could try to use File.download() and save it to File.applicationStorageDirectory, but I don't know if it will have any difference since it extends FileReference.

What I generally do is use URLStream instead of URLLoader. It gives you access to the raw bytes of the file you are downloading and then use File and FileStream

So something like (and this is untested off the top of my head, though I have used similar in the past):

var urlStream:URLStream = new URLStream();
urlStream.addEventListener(Event.COMPLETE, completeHandler);
urlStream.load(new URLLoader('url');

function completeHandler(e:Event):void {
    var bytes:ByteArray = new ByteArray();
    urlStream.readBytes(bytes);

    var f:File = File.applicationStorageDirectory.resolvePath('filename');
    var fs:FileStream = new FileStream();
    fs.open(f, FileMode.WRITE);
    fs.writeBytes(bytes);
    fs.close();
}

Now, obviously, there is a lot more you want to account for (errors, progress, etc). That should be enough to point you in the right direction, however.

It's possible to create a full download manager using this method (something I did for an iOS project two years ago), since you can save as-you-go to the file system rather than waiting until Event.COMPLETE fires (using the ProgressEvent.PROGRESS event). That allows you to avoid having a 500MB file in memory, something most devices can't handle.

Upvotes: 4

Related Questions