为什么当文件未完成加载时,URLStream完成事件会被分派? [英] Why does URLStream complete event get dispatched when the file is not finished loading?

查看:228
本文介绍了为什么当文件未完成加载时,URLStream完成事件会被分派?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个每天晚上连接到WordPress服务器的AIR kiosk应用程序,获取一个带有所有内容路径的JSON文件,然后下载该内容并将其保存到信息亭硬盘驱动器。

有几百个文件(jpg,png,f4v,xml),其中大部分是下载/保存的,没有任何问题。但是,有两个f4v文件永远无法完全下载。完整的事件会得到调度,但如果比较bytesTotal(来自进度事件)和bytesAvailable(来自整个事件),则它们不匹配; bytesTotal较大。 bytesTotal(来自progress事件)匹配服务器上的字节。

进度事件中的bytesLoaded永远不会增加到与bytesTotal相匹配的程度,所以我也不能依赖进度事件。这似乎每次都发生在相同的两个视频。视频不是很大,一个是13MB,另一个是46MB。我有更大的视频下载没有任何问题。编辑:重新启动我的电脑后,这两个视频现在完成下载,但我得到一个300kb的PNG文件相同的问题。



如果我将这个网址粘贴到Firefox,它会正确下载。我还写了一个简单的C#应用​​程序来下载文件,并能够毫无问题地下载它们,所以它似乎是Flash / AIR的问题。



编辑:这是一个简单版本的代码,这是从一个测试项目,这是唯一的代码(网址是在我们的本地网络,所以你将无法自己下载文件):$ / b>
$ b

  package {

import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
导入flash.net.URLStream;

[SWF(backgroundColor =#000000,frameRate =24,width =640,height =480)]

public class Test extends Sprite {

private var fileSize:Number;
private var stream:URLStream;
private var url:String =http://192.168.150.219/wordpress2/wp-content/uploads/2012/12/John-Butler-clip1.f4v;

public function Test(){
if(stage)
init();
else
this.addEventListener(Event.ADDED_TO_STAGE,init);


private function init(e:Event = null):void {
this.removeEventListener(Event.ADDED_TO_STAGE,init);
stream = new URLStream();
stream.addEventListener(ProgressEvent.PROGRESS,onLoadProgress);
stream.addEventListener(Event.COMPLETE,onLoadComplete);
stream.load(new URLRequest(url));


private function onLoadProgress(event:ProgressEvent):void {
fileSize = event.bytesTotal;
var percent:Number = event.bytesLoaded / event.bytesTotal * 100;
trace(percent +%); //这个永远不会达到100%b

私有函数onLoadComplete(event:Event):void {
trace(loaded,stream.bytesAvailable,of文件大小);
//输出13182905的13184365
//为什么当它没有被完全下载时是完整的?




$ div class =h2_lin>解决方案 http://www.newtonflash.com/blog/as3/prevent-xml-caching-problem/#comment-43

  {
var xmlPath:String =replaceYourXMLPathHere.xml
var urlReq:URLRequest = new URLRequest(xmlPath +?time = + new Date()。getTime());
}


I'm writing an AIR kiosk app that every night connects to a WordPress server, gets a JSON file with paths to all the content, and then downloads that content and saves it to the kiosk hard drive.

There's several hundred files (jpg, png, f4v, xml) and most of them download/save with no problems. However, there are two f4v files that never get downloaded completely. The complete event does get dispatched, but if I compare the bytesTotal (from the progress event) vs bytesAvailable (from the complete event) they don't match up; bytesTotal is larger. The bytesTotal (from the progress event) matches the bytes on the server.

The bytesLoaded in the progress event never increases to the point that it matches the bytesTotal either so I can't rely on the progress event either. This seems to happen on the same two videos every time. The videos are not very large, one is 13MB and the other is 46MB. I have larger videos that download without any problems.

EDIT: After rebooting my computer, the two videos now finish downloading but I'm getting the same problem with a 300kb png file.

If I paste the url into Firefox it downloads correctly. I've also written a simple c# app to download the files and it is able to download them with no problems, so it appears to be a problem with Flash/AIR.

EDIT: here's a simpler version of the code, this is from a test project and it's the only code (the url is on our local network so you won't be able to download the file yourself):

package  {

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.net.URLStream;

    [SWF(backgroundColor="#000000", frameRate="24", width="640", height="480")]

    public class Test extends Sprite {

        private var fileSize:Number;
        private var stream : URLStream;
        private var url:String = "http://192.168.150.219/wordpress2/wp-content/uploads/2012/12/John-Butler-clip1.f4v";

        public function Test() {
            if (stage)
                init();
            else
                this.addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event=null):void {
            this.removeEventListener(Event.ADDED_TO_STAGE, init);
            stream = new URLStream();
            stream.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
            stream.addEventListener(Event.COMPLETE, onLoadComplete);
            stream.load(new URLRequest(url));
        }

        private function onLoadProgress(event:ProgressEvent):void {
            fileSize = event.bytesTotal;
            var percent:Number = event.bytesLoaded / event.bytesTotal * 100;
            trace(percent + "%"); // this never gets to 100%
        }

        private function onLoadComplete(event:Event):void {
            trace("loaded", stream.bytesAvailable, "of", fileSize); 
            // outputs "loaded 13182905 of 13184365"
            // so why is it "complete" when it isn't fully downloaded?
        }
    }
}

解决方案

If anyone else has the same problem like I did. It turned out to be a caching problem which is present in AIR as well so a timestamp added to the request solves this: http://www.newtonflash.com/blog/as3/prevent-xml-caching-problem/#comment-43

{
var xmlPath:String="replaceYourXMLPathHere.xml"
var urlReq:URLRequest = new URLRequest(xmlPath+"?time=" + new Date().getTime());
}

这篇关于为什么当文件未完成加载时,URLStream完成事件会被分派?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆