如何检查是否未调度UPLOAD_COMPLETE_DATA事件?

时间:2010-09-02 15:20:48

标签: actionscript-3

有没有办法检查Flash中的UPLOAD_COMPLETE_DATA后是否未调度COMPLETE事件?

我正在处理文件上传器。它在文件之后上传文件 - 在COMPLETE下一个文件开始上传之后 - 对每个文件重复此操作。在COMPLETE上的最后一个文件中,如果没有下一个文件,则调度allCompleteHandler,如果发生某些错误,则会立即通过javascript向用户显示错误。

我在javascript中处理错误 - 它将每个错误存储到javascript中的变量中,并且在完成后它显示错误。

问题是:
我无法存储上一个文件中的错误(来自UPLOAD_COMPLETE_DATA的服务器的错误),因为这是在COMPLETE之后(在allCompleteHandler之后)调度的。

我需要解决这个问题,因为我不会为每个无效文件显示javascript警告框(例如,如果有100个文件)。

var parameters:Object = LoaderInfo(this.root.loaderInfo).parameters,

    fileFilter:Array,
    browseFilter:Array = [],

    files:FileReferenceList = new FileReferenceList(),
    selectedFiles:Array = [],
    file:FileReference = new FileReference(),

    url:URLRequest = new URLRequest(parameters.phpScript),
    uploadFolder,

    postMaxSize:Number,
    uploadMaxFilesize:Number,
    maxInputTime:int,

    speedTimer:Timer = new Timer(1000),
    uploadTimer:Timer = new Timer(60000, 1),

    count:int = 0,

    totalBytes:Number = 0,
    processedBytes:Number = 0,
    currentBytes:Number = 0,
    currentBytes2:Number = 0,
    lastBytes:Number = 0,
    uploadSpeed:Number = 0,

    inProgress:Boolean = false;

// Browse filter setup
fileFilter = ExternalInterface.call(parameters.fileManager + ".getFileFilter");

if (fileFilter) {
    for (var i:int = 0; i < fileFilter.length; i++) {
        browseFilter.push(new FileFilter(fileFilter[i][0], fileFilter[i][1]));
    }
}

function clickHandler(event:MouseEvent):void {
    if (!inProgress) {
        uploadFolder = ExternalInterface.call(parameters.fileManager + ".getCurrentFolder");

        if (uploadFolder != undefined) {
            files.browse(browseFilter);
        }
    }
}

stage.addEventListener(MouseEvent.CLICK, clickHandler);

function selectHandler(event:Event):void {
    var variables:URLVariables = new URLVariables();

    variables.folder = uploadFolder;
    url.data = variables;
    url.method = URLRequestMethod.POST;

    selectedFiles = files.fileList;

    postMaxSize = ExternalInterface.call(parameters.fileManager + ".getPostMaxSize");
    postMaxSize = postMaxSize ? postMaxSize : 50 * 1024 * 1024;

    uploadMaxFilesize = ExternalInterface.call(parameters.fileManager + ".getUploadMaxFilesize");
    uploadMaxFilesize = uploadMaxFilesize ? uploadMaxFilesize : 50 * 1024 * 1024;

    maxInputTime = ExternalInterface.call(parameters.fileManager + ".getMaxInputTime");
    maxInputTime = maxInputTime ? maxInputTime : 60;

    // Get total size of selected files
    for (var i:int = 0; i < selectedFiles.length; i++) {
        totalBytes += selectedFiles[i].size;
    }

    ExternalInterface.call(parameters.fileManager + ".selectHandler", {
        selectedFiles : selectedFiles,
        totalBytes    : totalBytes
    });

    // Start upload process
    inProgress = true;
    speedTimer.start();
    upload();
}

files.addEventListener(Event.SELECT, selectHandler);

function upload():void {
    uploadTimer.reset();

    currentBytes2 = 0;

    if (count) {
        processedBytes += file.size;

        if (currentBytes < processedBytes) {
            currentBytes = processedBytes;
        }
    }

    if (selectedFiles.length) {
        file = FileReference(selectedFiles.shift());
        count++;

        ExternalInterface.call(parameters.fileManager + ".beforeUploadHandler", {
            file         : file,
            currentBytes : currentBytes
        });

        if (file.size <= postMaxSize) {
            if (file.size <= uploadMaxFilesize) {
                file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                file.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
                file.addEventListener(Event.OPEN, openHandler);
                file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                file.addEventListener(Event.COMPLETE, completeHandler);
                file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);

                file.upload(url);
            } else {
                ExternalInterface.call(parameters.fileManager + ".uploadMaxFilesizeHandler", file);
                upload();
            }
        } else {
            ExternalInterface.call(parameters.fileManager + ".postMaxSizeHandler", file);
            upload();
        }
    } else {
        ExternalInterface.call(parameters.fileManager + ".allCompleteHandler", {
            currentBytes : currentBytes
        });

        speedTimer.stop();

        count = 0;

        totalBytes = 0;
        processedBytes = 0;
        currentBytes = 0;
        lastBytes = 0;
        uploadSpeed = 0;

        inProgress = false;
    }
}

function securityErrorHandler(event:SecurityErrorEvent):void {
    ExternalInterface.call(parameters.fileManager + ".securityErrorHandler", event);
}

function httpStatusHandler(event:HTTPStatusEvent):void {
    ExternalInterface.call(parameters.fileManager + ".httpStatusHandler", event);
    selectedFiles = [];
}

function openHandler(event:Event):void {
    ExternalInterface.call(parameters.fileManager + ".openHandler", event);

    uploadTimer.delay = maxInputTime * 1000;
    uploadTimer.start();
}

function ioErrorHandler(event:IOErrorEvent):void {
    ExternalInterface.call(parameters.fileManager + ".ioErrorHandler", event);
    upload();
}

function progressHandler(event:ProgressEvent):void {
    currentBytes += event.bytesLoaded - currentBytes2;
    currentBytes2 = event.bytesLoaded;

    ExternalInterface.call(parameters.fileManager + ".progressHandler", {
        current      : event,
        currentBytes : currentBytes
    });
}

function completeHandler(event:Event):void {
    ExternalInterface.call(parameters.fileManager + ".completeHandler", event);
    upload();
}

function uploadCompleteDataHandler(event:DataEvent):void {
    ExternalInterface.call(parameters.fileManager + ".uploadCompleteDataHandler", "(" + event.data + ")");
}

function updateUploadSpeed(event:TimerEvent):void {
    if (currentBytes > lastBytes) {
        uploadSpeed = currentBytes - lastBytes;

        ExternalInterface.call(parameters.fileManager + ".uploadSpeedHandler", uploadSpeed);

        lastBytes = currentBytes;
    }
}

speedTimer.addEventListener(TimerEvent.TIMER, updateUploadSpeed);

function maxInputTimeHandler(event:TimerEvent):void {
    ExternalInterface.call(parameters.fileManager + ".maxInputTimeHandler", file);
}

uploadTimer.addEventListener(TimerEvent.TIMER, maxInputTimeHandler);

function cancelUpload():void {
    file.cancel();

    selectedFiles = [];
    upload();
}

ExternalInterface.addCallback("cancelUpload", cancelUpload);

我可以通过设置我的PHP脚本来始终返回数据并检查这些数据并使用UPLOAD_COMPLETE_DATA开始下一个文件上传,但我不喜欢这个(它可能很慢,也许我认为)。 ..

这是一个简单的问题,但也许很难解释为什么我需要它。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

好的,http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/net/FileReference.html#event:progress

怎么样?

和样本

http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/net/FileReference.html#includeExamplesSummary

http://blog.flexexamples.com/2007/10/30/using-for-the-filereference-classs-uploadcompletedata-event-to-capture-data-from-a-server-side-script/

http://bcdef.org/2006/12/09/new-event-for-as3-filereference-uploadcompletedata/

来自docs

uploadCompleteData:

在成功上载后从服务器收到数据后调度。如果未从服务器返回数据,则不会调度此事件。

完成:

下载完成或上载生成HTTP状态代码200时调度。对于文件下载,当Flash Player或Adobe AIR完成将整个文件下载到磁盘时,将调度此事件。对于文件上载,在Flash Player或Adobe AIR从接收传输的服务器收到HTTP状态代码200后调度此事件。

因此,如果您正在上传文件而不期望服务器提供任何类型的响应,那么您可以使用完整版。但是,如果您希望服务器在上传的响应中向您返回数据,那么您需要等待uploadCompleteData。