Apache flex Flex 4.6移动应用程序中的进度事件

Apache flex Flex 4.6移动应用程序中的进度事件,apache-flex,flex4.5,flexbuilder,Apache Flex,Flex4.5,Flexbuilder,我正在开发一个Flex移动应用程序,需要显示图像的上传进度 代码是: protected function upload( ba:ByteArray, fileName:String = null ):void { if( fileName == null ) { var now:Date = new Date(); fileName = "IMG" + now.fullYea

我正在开发一个Flex移动应用程序,需要显示图像的上传进度

代码是:

protected function upload( ba:ByteArray, fileName:String = null ):void {
            if( fileName == null ) {                
                var now:Date = new Date();
                fileName = "IMG" + now.fullYear + now.month +now.day +
                    now.hours + now.minutes + now.seconds + ".jpg";
            }

            var loader:URLLoader    = new URLLoader();
            loader.dataFormat       = URLLoaderDataFormat.BINARY;

            var wrapper:URLRequestWrapper = new URLRequestWrapper(ba, fileName, null, params);
            wrapper.url = "http://www.the_url_to_upload.com/php_content/upload_image.php";

            loader.addEventListener( Event.COMPLETE,            completeImageHandler );
            loader.addEventListener( ProgressEvent.PROGRESS,    imageProgress);
            loader.addEventListener(IOErrorEvent.IO_ERROR,      errorImageUploading );
            loader.load(wrapper.request);
        }
        private function imageProgress(evt:ProgressEvent):void {
            var pcent:Number=Math.floor(evt.bytesLoaded/evt.bytesTotal*100);
            label_upload.text = pcent+"%";
        }
我有一个名为“Label_upload”的标签,它应该显示文件上传时进度的百分比

事实上,所有这些都可以正常工作,但进度事件不会改变任何事情。始终显示0%

我猜不出我的错


谢谢。

Flash不提供上传文件的进度事件-仅提供下载

如果您需要进度事件,则必须将文件拆分为多个部分,并一次上载每个部分;手动更新进度消息以响应每个部分的完成事件。例如:

//assume file to upload stored as ByteArray called "ba"

//setup counters
currentChunk = 0;
totalChunks = Math.ceil(ba.length/CHUNK_SIZE);
//listener
loader.addEventListener(Event.COMPLETE, completeHandler);
此代码将发送单个块:

function sendChunk():void
{
    const CHUNK_SIZE:int = 4096;
    var request:URLRequest = new URLRequest(destinationUrl);
    request.method = URLRequestMethod.POST;
    request.contentType = "application/octet-stream";
    request.data = new ByteArray();
    var chunkSize:uint = Math.min(CHUNK_SIZE, ba.bytesAvailable);
    ba.readBytes(request.data as ByteArray, 0, chunkSize);
    loader.load(request);
}
CHUNK_SIZE是一次性发送的最大字节数。 request.contentType=。。。将数据格式设置为二进制

然后:

函数completeHandler(事件:event):void
{
//期望服务器的结果确认数据的接收
if(loader.data==“OK”)
{

如果(++CurrentChunk看不到代码有任何问题。是否调用imageProgress方法?请添加“trace(evt.bytesLoaded);”为了确认它,我认为它没有被调用,因为标签是上传的。文本肯定会在文本中更新。尽管如此,我会跟踪它。它没有更新,所以永远不会调用imageProgress事件。但是completeImageHandler会触发吗?加载的图像文件有多大?也许它们足够小,可以一次性完全加载(因此没有进度)是的,图像上传到服务器,所有过程都很好,它只是通过progressEvent显示进度百分比,而progressEvent不会启动。那么,您如何将多媒体元素(图像、视频等)拆分为多个部分进行上传,然后在PHP中加入它们?这听起来很简单,但实际上并不可行。
function completeHandler(event:Event):void
{
    //expect a result from server to acknowledge receipt of data
    if (loader.data=="OK")
    {
        if (++currentChunk<totalChunks)
        {
    trace("progress: "+currentChunk+" of "+totalChunks+" sent");
            //send next chunk
            sendChunk();
        }
        else
        {
             trace("finished!");
        }
    }
    else
    {
        trace("OK not receieved from server");
    }
}