0

我正在编写此代码以将文件上传到服务器用户闪存,它将文件发送到服务器,但没有显示任何进度,

var _var1:String= '';

            var globalParam = LoaderInfo(this.root.loaderInfo).parameters;
            for (var element:String in globalParam) {
            if (element == 'host'){
            _var1 =   globalParam[element];
                }
            }


            if(_var1 != '')
            {
                var req:URLRequest = new URLRequest(_var1);
                req.contentType = 'application/octet-stream';
                req.method = URLRequestMethod.POST;
                req.data = recorder.output;




                var loader:URLLoader = new URLLoader(req);
                // set progress
                loader.addEventListener(ProgressEvent.PROGRESS , fileUploadProgress);

                // set on complete
                loader.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA , fileUploadComplete);

                // set on error handler
                loader.addEventListener(IOErrorEvent.IO_ERROR , fileUploadIOError);

                // set on security error handler
                loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR , fileUploadSecurityHandler);

                // set on open
                loader.addEventListener(Event.OPEN , fileUploadOpenHandler);

                //ExternalInterface.call("$.jRecorder.callback_upload_progress_change" , String(0));
                function fileUploadProgress(event:ProgressEvent):void{
                    var bytesTotal:uint = event.bytesTotal;
                    var bytesLoaded:uint = event.bytesLoaded;
                    var pctDone:Number = (bytesLoaded / bytesTotal) * 100;
                    ExternalInterface.call("$.jRecorder.callback_upload_progress_change" , String(pctDone));
                    //trace(bytesLoaded+ " / "+bytesTotal); 
                }


                // On open
                function fileUploadOpenHandler(event:Event):void{

                    ExternalInterface.call("$.jRecorder.callback_upload_started"); 
                }

                function fileUploadComplete(event:DataEvent):void{

                    ExternalInterface.call("$.jRecorder.callback_upload_finished" , String(event.currentTarget.size));; 
                }


                function fileUploadSecurityHandler(event:SecurityErrorEvent):void{
                    ExternalInterface.call("$.jRecorder.callback_upload_securityproblem" , String(event.text));; 
                }


                function fileUploadIOError(event:IOErrorEvent):void{
                    ExternalInterface.call("$.jRecorder.callback_upload_ioproblem" , String(event.text));; 
                }
            }

当文件开始上传时,fileOpenHandler 到达,结束后,我得到“无限”,我无法获得任何进展或完成,我的代码哪里有问题?

4

1 回答 1

-1

您无法使用 URLLoader 获取进度事件。http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html#event:progress上的文档提到了这一点。如果您想接收进度事件,则必须使用另一种方法来发送数据(尽管我不知道那可能是什么)。

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;

    public class URLLoaderProgress extends Sprite
    {
        public function URLLoaderProgress()
        {
            var loader:URLLoader;
            var request:URLRequest;
            var data:String;

            request = new URLRequest("http://example.com");
            request.contentType = "application/octet-stream";
            request.method = URLRequestMethod.POST;
            for (var i:int = 0; i < 10; i++)
            {
                data += "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?";
            }

            loader = new URLLoader(request);
            loader.addEventListener(ProgressEvent.PROGRESS, function (e:ProgressEvent):void
            {
                trace(e.bytesLoaded);
            });

            loader.addEventListener(Event.COMPLETE, function (e:Event):void
            {
                trace("complete");
            });
        }
    }
}
于 2013-08-01T07:15:03.607 回答