LordZardeck
LordZardeck

Reputation: 8293

Save a download without filereference

Is there anyway to download a file with the URLLoader and then save it to the disk without using filereference or anything that uses a dialog? This is what I have but isn't working:

public function onDownloadComplete(e:Event):void
{
    DownloaderProgress.label = 'Download Done';

    var loader:URLLoader = URLLoader(e.target);
    var rtsFile:File = File.applicationStorageDirectory.resolvePath("RTS.zip");
    var rtsStream:FileStream = new FileStream();
    rtsStream.open(rtsFile, FileMode.WRITE);
    rtsStream.writeBytes(loader.data);
    rtsStream.close();
}

Also to clarify, My program is in adobe air. So it will be ran as a desktop application not a flash object on a webpage.

Upvotes: 2

Views: 2481

Answers (2)

crooksy88
crooksy88

Reputation: 3851

I believe you might be able to do it if you use a packager like SWFStudio.

http://www.northcode.com/forums/showthread.php?t=10108&highlight=download

Upvotes: 0

Philipp Kyeck
Philipp Kyeck

Reputation: 18860

you have to use the URLStream instead of the URLLoader:

var downloadStream:URLStream = new URLStream();
downloadStream.addEventListener(Event.COMPLETE, onDownloadComplete);
downloadStream.load(new URLRequest(url));

// ...

private function onDownloadComplete(event:Event):void
{
    var bytes:ByteArray = new ByteArray();
    downloadStream.readBytes(bytes);

    try
    {           
        // delete old file first
        if (_saveToFile.exists)
        {
            _saveToFile.deleteFile();
        }
        _fs = new FileStream();
        _fs.addEventListener(IOErrorEvent.IO_ERROR, onSaveFileIOError);
        _fs.open(_saveToFile, FileMode.WRITE);
        _fs.writeBytes(bytes);
        _fs.close();

        _fs.removeEventListener(IOErrorEvent.IO_ERROR, onSaveFileIOError);
    }
    catch (error:Error)
    {
        trace("could not write file to local machine");
    }
}

i just copy&pasted some code from a class of mine. not 100% complete but should point you in the right direction...

Upvotes: 6

Related Questions