Im looking for a simple way to find the file sizes of the files i planning on loading without loading them yet. I'm basically making a batch of files that needs to be loaded and it would be very useful if I could determine on beforehand how big the total load is going to be.
the Loader object in flash has a totalBytes property, but that will only return a value when you are already loading a file.
I don't know how you would do this with actionscript (I'm no Guru) but my first guess would be to use a PHP script (or any other scripting language of your choice) to get the file sizes and return the values.
Another (probably overkill) way would be to create something that simply asks the server for the headers of the file (including the filesize). The same could be achieved by simply starting to download the file and closing the stream when you have the filesize (should be pretty fast) or having (as suggested before) some other way of asking the server for the size for the file (useful if you're loading multiple files and need to know the filesize before loading)
There is no way to getting the size of a file before from
Actionscript 3
. A easy solution would be to add a small script to the server which scans for all files and returns their names and their size to the swf (asJSON
orXML
or whatever).By loaded, do you mean entirely loaded, or with no load call? If a load call is acceptable, the following could be useful. The progress event gives the incoming data's Total size on the first fire. We merely let a given progress event fire, take the total value of the current loading item and add that to our own _total variable; We continue this until our dataprovider (array, xml, etc) is empty. by the end you have the sum of all your assets totals.
So we load an asset just long enough to append its Size total, to our own _total variable and then we call the next item in the array and append its total until the data provider is empty. Obviously this could be done with XML, or however you wish, an array is just what I used for the exercise.
I have experimented with the method provided by Brian Hodge, as well as another method in which I sent variables (folder locations as strings from XML) to PHP in order to get the combined size of all files within a particular folder and return the data to Flash.
One major problem I encountered on my journey to preloading paradise was that Flash Player seems to produce inconsistent results with regard to file size! Even if I loaded exactly the same files twice Flash would tell me they were different sizes... sometimes.
This also caused problems with the alternate (PHP) method, since the PHP script would produce (seemingly) accurate file size results that were inconsistent with those produced by Flash, causing my loader to go to 85% or 128%, dependant on caching and Flash's mood :/
I scrapped both methods in favour of a nice class called bulk-loader, which allows for queued loading and progress tracking of multiple items, and much more :D
Just be aware that it will increase your swf file size significantly... how ironic
There is no way to do this in ActionScript. You'll need some server side code to do this. You could create a method that takes a filename and returns the total bytes.