gpt4 book ai didi

actionscript-3 - 带宽不足时,AIR AS3文件下载已损坏

转载 作者:行者123 更新时间:2023-12-04 04:38:06 27 4
gpt4 key购买 nike

我正在使用Downloader类从WS2012上的IIS服务器获取大文件并处理下载进度。

它可以正常工作,但是当客户端的带宽太饱和时,不再触发Progress事件,并且经过一定时间后下载会停止(Complete事件似乎已触发?),尽管下载未完成,但仍然客户端文件损坏。

我找不到解决方案,甚至找不到解决该问题的策略(完成下载并显示错误?等待带宽可用以获取下一个字节?)

这是Downloader.as类

public class Downloader extends EventDispatcher
{
[Event(name="DownloadComplete", type="DownloadEvent")]

public static var spd:int = 0;
private var file:File;
private var fileStream:FileStream;
private var url:String;
private var urlStream:URLStream;

var mc_background:MovieClip;
var howManyTimes:Number = 3; //How many times per second the download speed will be traced
var bytesLoaded:Number = 0; //don't change, necessary for calculation
var lastTime:int = 0; //don't change, necessary for calculation


private var waitingForDataToWrite:Boolean = false;

public function Downloader(s:MovieClip)
{
mc_background = s;

lastTime = getTimer();
urlStream = new URLStream();

urlStream.addEventListener(Event.OPEN, onOpenEvent);
urlStream.addEventListener(ProgressEvent.PROGRESS, onProgressEvent);
urlStream.addEventListener(Event.COMPLETE, onCompleteEvent);

fileStream = new FileStream();
fileStream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, writeProgressHandler)

}

public function download(formUrl:String, toFile:File):void {
this.url = formUrl;
this.file = toFile;
mc_background.pb.file_txt.text = file.name;
fileStream.openAsync(file, FileMode.WRITE);
urlStream.load(new URLRequest(url));
}

private function onOpenEvent(event:Event):void {
waitingForDataToWrite = true;

dispatchEvent(event.clone());
}

private function onProgressEvent(event:ProgressEvent):void {
var time:int = getTimer();
if(time - lastTime >= (1000/howManyTimes))
{
var kiloBytes:Number = (event.bytesLoaded - bytesLoaded)/1000;
var timeInSecs:Number = (time - lastTime)/1000;

var kbsPerSecVal:Number = Math.floor(kiloBytes/timeInSecs);
trace(kbsPerSecVal + " kbs/s");

mc_background.pb.speed_txt.text = kbsPerSecVal + " kbs/s";
bytesLoaded = event.bytesLoaded;
lastTime = getTimer();
}
if(waitingForDataToWrite){
writeToDisk();
dispatchEvent(event.clone());
}
}

private function writeToDisk():void {
var fileData:ByteArray = new ByteArray();
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
fileStream.writeBytes(fileData,0,fileData.length);
waitingForDataToWrite = false;

dispatchEvent(new DataEvent(DataEvent.DATA));
}

private function writeProgressHandler(evt:OutputProgressEvent):void{
waitingForDataToWrite = true;
}

private function onCompleteEvent(event:Event):void {
if(urlStream.bytesAvailable>0)
writeToDisk();
fileStream.close();

fileStream.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS, writeProgressHandler);

dispatchEvent(event.clone());
// dispatch additional DownloadEvent
dispatchEvent(new DownloadEvent(DownloadEvent.DOWNLOAD_COMPLETE, url, file));
}

}

最佳答案

尝试使用存储预期文件大小的int变量。

现在,当事件完成触发时,检查您的[下载] fileData.length是否等于预期的FileSize。

如果少于,则重试,直到相等或足够好。要从最后的字节数中获取,您可以在请求 header 中使用“范围请求”。该链接还会显示该链接。如果这样做对您有帮助。

就像是URLRequestHeader("range","bytes="+startPOS+"-"+endPOS);,其中startPOS为filedata.length + 1,endPos为Expected_FileSize量。

关于actionscript-3 - 带宽不足时,AIR AS3文件下载已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31784235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com