我正在编写一个流程,用户需要选择一个远远超出其可用 RAM 的文件,并将该文件分成小块(用于上传)。
我能够创建对所述文件的文件引用,但是当我尝试将其传递给 fileStream 时,它似乎尝试在对其进行操作之前尝试将其读入内存。
有没有办法让 fileStream 仅获取对文件的引用,然后按照记录的方式使用 readBytes?
这是我的代码...当用户在浏览器对话框中选择文件时调用它。
private function selectHandler(event:Event):void {
var file:File = File(event.target);
trace("selectHandler: name=" + file.name );
var stream:FileStream = new FileStream();
var f:File = event.target as File;
stream.open(f, FileMode.READ); //here the process will lock up if the file you pass it is too large.
var bytes:ByteArray = new ByteArray();
stream.readBytes(bytes,0,1024);
trace(bytes);
stream.close();
}
非常感谢,提前。
最佳答案
那么,解决方案...
stream.readAhead = 10000;// some reasonable number
stream.openAsync(f, FileMode.READ); //here the process will no longer lock up, if the above chunk is set to a number that Flash can handle.
//then in your PROGRESS listener you read the bytes into a byteArray
if(target.availableBytes >= 10000){//you need this because progress gets called many times before the full chunk is read. You only want to use it when you have the full chunk. (you also will want to keep track of the total read and when that total + chunk > fileSize, you'll want to adjust your chunk to read in that last bunch of bytes.
var bytes:ByteArray = new ByteArray();
stream.readBytes(bytes,0,event.target.availableBytes); //this will provoke the next segment to get read as well
}
关于flash - AS3 fileStream出现将文件读入内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4787805/