Jump to content
Search Community

Loading multiple files and storing to disc

crooksy88 test
Moderator Tag

Recommended Posts

HI,

 

I need to download several files (.flv, .xml and .mp3) and store them to disc (not display or play them) but am having trouble doing this.

 

As the queue loads and each child is loaded I am calling the 'saveFile' function in which I need to get the loaders' content as a ByteArray so I can save it with FileStream.

 

Any advice would be very much appreciated.

 

Here's where I am at the moment:

private var _queue:LoaderMax;
private var _dataD:ByteArray;

private function startAndroidDownload():void {


LoaderMax.activate([XMLLoader, BinaryDataLoader, VideoLoader, MP3Loader]);


_filesToDownload = ["squats.flv","Workout1.xml","Workout2.xml","Workout3.xml","Workout4.xml","Workout5.xml","Workout6.xml","Workout7.xml","Workout8.xml","Workout9.xml"];


_numFiles = _filesToDownload.length;


_queue = LoaderMax.parse(_filesToDownload,{maxconnections:1, onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler, onChildComplete:saveFile});


_queue.prependURLs("http://www.mydomain.com/androidIncludes/");


_queue.load();


}


private function progressHandler(event:LoaderEvent):void {
//show progress
}


private function completeHandler(event:LoaderEvent):void {
//queue complete
}


private function errorHandler(event:LoaderEvent):void {
//handle error
}

private function saveFile(event:LoaderEvent):void {


// I'm wanting to get the loader data as a ByteArray so I can save it to disc with FileStream
_dataD = _queue.getContent(event.target.name) as ByteArray;




}
Link to comment
Share on other sites

Thanks Carl,

 

That seems to be working although I have a slight issue.

 

I think that my app is running out of memory as I try to load the LoaderMax queue (290MB of assets).

 

As each file is downloaded and stored to disc I was hoping to remove that DataLoader from the queue but doing so seems to reset the overall progress of the queue.

private function startAndroidVideosDownload():void {

_dFile = File.documentsDirectory.resolvePath("includes");

if (!_dFile.exists) {
_dFile.createDirectory();
}

_numFilesLoaded = 0;
_filesToDownload = [];

for(var i:int = 0; i < DataStore._appData.androidIncludes.item.length(); i++) {

_filesToDownload.push([DataStore._appData.androidIncludes.item[i], DataStore._appData.androidIncludes.item[i].@bytes]);

}

_numFiles = _filesToDownload.length;

log("_numFiles "+_numFiles);

_queue = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler, onChildComplete:saveFile, maxconnections:2});

for (i = 0; i < _numFiles; i++) {
_queue.append( new DataLoader(_filesToDownload[i][0], {name:_filesToDownload[i][0], format:"binary", estimatedBytes:_filesToDownload[i][1]}) );
}

_queue.prependURLs("http://www.domain.com/mobileSupport/androidIncludes/");

_queue.load();

}
private function saveFile(event:LoaderEvent):void {

_dataD = LoaderMax.getContent(event.target.name);

_fileStream = new FileStream();
_dFile = File.documentsDirectory.resolvePath("includes/"+event.target.name);

_fileStream.openAsync(_dFile, FileMode.WRITE); 
_fileStream.writeBytes(_dataD, 0, _dataD.length); 
_fileStream.close();

//now dispose the loader 
var currentLoader:DataLoader = LoaderMax.getLoader(event.target.name);
currentLoader.dispose(true);
currentLoader = null;

}

Would you have any suggestions on the most efficient way to download 90 files (totalling 290MB)? Being able to show the total progress is a big plus.

 

Thanks,

 

Mark

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...