Flash,在实际加载文件之前访问totalBytes
我正在寻找一种简单的方法来查找我计划加载的文件的文件大小,而无需加载它们。 我基本上正在制作一批需要加载的文件,如果我可以事先确定总负载有多大,那将非常有用。
flash 中的 Loader 对象有一个totalBytes 属性,但只有当您已经加载文件时才会返回一个值。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
所谓加载,你的意思是完全加载,还是没有加载调用? 如果加载调用可以接受,则以下内容可能有用。 进度事件在第一次触发时给出传入数据的总大小。 我们只需触发给定的进度事件,获取当前加载项的总值并将其添加到我们自己的 _total 变量中; 我们继续这个操作,直到我们的数据提供者(数组、xml 等)为空。 最后你就得到了所有资产的总和。
因此,我们加载一个资产足够长的时间,以便将其总大小附加到我们自己的 _total 变量中,然后调用数组中的下一项并附加其总大小,直到数据提供程序为空。 显然,这可以使用 XML 来完成,或者无论您希望什么,数组正是我在练习中使用的。
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.
以前无法从
Actionscript 3
获取文件的大小。 一个简单的解决方案是向服务器添加一个小脚本,该脚本扫描所有文件并将其名称和大小返回到 swf(作为JSON
或XML
或其他形式) 。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).在 ActionScript 中没有办法做到这一点。 您需要一些服务器端代码来执行此操作。 您可以创建一个接受文件名并返回总字节数的方法。
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.
我不知道你会如何使用actionscript(我不是专家)来做到这一点,但我的第一个猜测是使用PHP脚本(或你选择的任何其他脚本语言)来获取文件大小并返回值。
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)
我已经尝试过 Brian Hodge 提供的方法,以及另一种方法,其中我将变量(XML 中的字符串形式的文件夹位置)发送到 PHP,以便获取特定文件夹内所有文件的组合大小并将数据返回到闪光。
我在预加载天堂之旅中遇到的一个主要问题是 Flash Player 似乎在文件大小方面产生不一致的结果! 即使我加载完全相同的文件两次,Flash 也会告诉我它们的大小不同......有时。
这也导致了替代 (PHP) 方法的问题,因为 PHP 脚本会生成(看似)准确的文件大小结果,但与 Flash 生成的结果不一致,导致我的加载程序达到 85% 或 128%,具体取决于缓存和Flash 的心情:/
我放弃了这两种方法,转而使用一个名为 bulk-loader,它允许排队加载和跟踪多个项目的进度,等等 :D
请注意,它会显着增加您的 swf 文件大小...多么讽刺
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