<p>Alright, currently I have my SWF hitting a php file that will go and find all the files I specify to it, add their sizes together and return their combined sizes as one number. I then use this number with a ProgressEvent listener to determine the current percentage of files downloading for that particular section of the website.</p>

<p>Pretty straightforward right? Well now using that PHP file is out of the question and I'm attempting to do everything inside the SWF instead of having it hit an outside script to get the numbers I need.</p>

<p>Is there any good way to get a file's size BEFORE I start loading it into flash? I really need the preloader to be a 0 to 100% preloader so I need the total number of bytes I will be downloading before it actually starts.</p>

<p>One thought I had was to just go through the array holding the file URLs, start loading them, getTotalBytes without displaying any loading, kill the load on the first tick, add up all those total bytes numbers, and then start the actual downloading process. This method seems very ugly and it will be a huge time waste as every time the user hits a pre loader on the site it will probably take a second or two to run through all the files, find their total and then actually start downloading. </p>

<p>Is there a better solution to this problem without going outside of flash to get the size of those files?</p>

[{'answer_id': 67646, 'author': 'mikechambers', 'author_id': 10232, 'author_profile': 'https://Stackoverflow.com/users/10232', 'pm_score': 3, 'selected': True, 'text': '<p>You could do an HTTP HEAD request to the server for the files. This will return the header info (including file size) but not the actual file. You could then make a GET request to load the files.</p>\n\n<p><a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html" rel="nofollow noreferrer">http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html</a></p>\n\n<p>(Check out section 9.4 HEAD)</p>\n\n<p>What I would probably do is a two tier progress bar (with 2 progress bars). One showing overall progress (0 to 100%) and one showing per file progress (as each file is downloaded).</p>\n\n<p>That way, as long as you know the number of files to load, you can do the total progress without first having to hit the server to get the file sizes.</p>\n\n<p>mike chambers</p>\n\n<p>mesh@adobe.com</p>\n'}, {'answer_id': 68111, 'author': 'Antti', 'author_id': 6037, 'author_profile': 'https://Stackoverflow.com/users/6037', 'pm_score': 1, 'selected': False, 'text': '<p>Mike Chamber\'s idea will help you but it will still be slower than using something serverside, since you\'ll have to make a request for each file anyway. It\'s essentially the same of what you\'re saying yourself, but when you\'re explicitly asking for the headers it will be slightly faster. Use a Socket to do the request:</p>\n\n<pre>var socket : Socket = new Socket( );\nsocket.addEventListener( Event.CONNECT, connectHandler );\nsocket.addEventListener( ProgressEvent.SOCKET_DATA, dataHandler );\nsocket.connect( "yourserver.com", 80 );\n\nfunction connectHandler( event : Event ) : void {\n    var headers : String = "HEAD yourfilename.ext http/1.1\\r\\n";\n    headers += "Host: yourserver.com\\r\\n";\n    headers += "\\r\\n\\r\\n";\n    socket.writeUTFBytes( headers );\n    socket.flush( );\n}\n\nfunction dataHandler( event : ProgressEvent ) : void {\n    trace( "Received headers\\n" );\n    trace( socket.readUTF( socket.bytesAvailable ) );\n}\n</pre>\n'}, {'answer_id': 68982, 'author': 'fenomas', 'author_id': 10651, 'author_profile': 'https://Stackoverflow.com/users/10651', 'pm_score': 0, 'selected': False, 'text': "<p>If it's absolutely necessary to control how the files are loaded, then I believe Mike Chambers' suggestion to make an HTTP HEAD request is the way to go.</p>\n\n<p>However, unless there's a good reason not to, I'd simply begin loading all the files at once and get my file sizes from each file's getBytesTotal method. Since Flash gets its network stack from the browser, the number of files actually loaded concurrently will conform to the (user-definable) browser settings.</p>\n"}, {'answer_id': 126027, 'author': 'Brian Hodge', 'author_id': 20628, 'author_profile': 'https://Stackoverflow.com/users/20628', 'pm_score': 0, 'selected': False, 'text': "<p>The following is just for example purposes.  In a real case I wouldnt use Timer, I would have an array or XML object to iterate through with a for loop, regardless it works great and basically once you hit the end of your loop (e.g. if(i == (length - 1)) have it call the functionality that starts the actual pre-loading now that we have our total.  Now we iterate through the array or XML object again, but this time we do it only once each asset has loaded, not in a for loop.  This asynchronous method can then sit there and compare how much data it has loaded and divide that by the earlier found total giving you your percentage.</p>\n\n<pre>\nvar totalBytes:Number = 0;\n\nvar loader:Loader = new Loader();\nvar request:URLRequest = new URLRequest();\nrequest.url = 'badfish.jpg';\n\nvar timer:Timer = new Timer(200);\ntimer.addEventListener(TimerEvent.TIMER, onTimer);\n\nfunction onTimer(e:TimerEvent)\n{\n    loader.load(request);\n    loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);\n}\nfunction onProgress(e:ProgressEvent):void\n{\n    totalBytes+=e.bytesTotal;\n    trace(e.bytesTotal,totalBytes);\n    loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);\n}\n\ntimer.start();\n</pre>\n"}]
['https://Stackoverflow.com/questions/67556', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1945/']