2013-02-11 93 views
0

我只是开发一个Flex移动应用程序,需要显示的图像的上传进度。进度事件中的Flex 4.6移动应用程序

的代码是:

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%。

我猜不出我的错。

谢谢。

+0

不能看到与代码的任何问题。 imageProgress方法是否被调用?添加“trace(evt.bytesLoaded);”对其进行确认 – 2013-02-12 16:58:37

+0

我认为它不会被调用,因为label_upload.text肯定会在文本中更新。所有相同的我会跟踪它。 – Apalabrados 2013-02-12 18:17:22

+0

它没有更新,所以imageProgress事件从不被调用。 – Apalabrados 2013-02-13 20:24:26

回答

1

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 = ...将数据格式设置为二进制。

然后:

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"); 
    } 
} 

这将在部分发送整个文件。 php脚本应该以“OK”(或选择其他适当的响应)作为响应 - 这将在loader.data中显示 - 所以Flash知道没有错误。

我不能帮助你处理事情的php方面,因为我一直把它留给别人,但它很直截了当,因为我知道它,所以在堆栈上的问题应该会给你一个答案。

+0

那么,如何将多媒体元素(图像,视频等)分成上传部分,然后加入他们的PHP?这听起来很容易,但不是在实践中。 – Apalabrados 2013-02-13 20:43:38

+0

扩大了我的答案 – 2013-02-13 21:09:00

相关问题