0

How to display the progress on a data transfer between Flash and PHP? Below is the AS3 code I'm using to upload a base64 encoded image through PHP.

var scriptLoader:URLLoader = new URLLoader();
var scriptVars:URLVariables = new URLVariables();

var scriptRequest:URLRequest = new URLRequest("https://www.example.com/sendit.php");

var imagedata = Base64.encode(mybitmap);
scriptVars.theimage = imagedata

scriptRequest.method = URLRequestMethod.POST;
scriptRequest.data = scriptVars;
scriptLoader.load(scriptRequest);

(Server is running PHP Version 5.3.10)

Tom
  • 5,588
  • 20
  • 77
  • 129
  • You can make that visible with a network sniffer. – hakre Apr 12 '12 at 18:41
  • FileReference.upload should have what you want – The_asMan Apr 12 '12 at 19:05
  • @The_asMan How can it be done, with **read-only** 'data' ? http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#data – Engineer Apr 12 '12 at 19:09
  • @Tom URLLoader does not support events on the upload it only supports events on the download side of a page request. – The_asMan Apr 12 '12 at 21:44
  • @Engineer ah my bad I miss read the question. – The_asMan Apr 12 '12 at 22:33
  • 1
    @Tom you are limited on upload progress since AS3 does not support it very well at all. I believe you might be able to use a socket connection to get the data. or you can look into this method http://www.ibm.com/developerworks/library/os-php-v525/index.html – The_asMan Apr 12 '12 at 22:37
  • Actually I use the FileReference and ProgressEvent listener to get the uploaded data size and it seems to work fine, only caveat being the server does some extra work after the file has finished uploading from the client so we have it update to 80% using the progress events loaded/total*80 then just jump the progress bar to 100% once the complete event is received indicating the server has completed it's work and returned a response. Not sure what all the chatter is about it not working for uploads, where is this idea coming from? – shaunhusain Apr 12 '12 at 22:42
  • @The_asMan So I need to send the progress from PHP to Flash? Is there an AS3 example or tutorial available? – Tom Apr 13 '12 at 15:28
  • @shaunhusain I can't use FileReference for this. – Tom Apr 13 '12 at 15:28
  • @Tom okay I realized that you weren't using it after I posted the comment, sorry for the noise. I'd say next best bet is to have the service write the progress out to a file and have another php file that you poll from the front-end to get progress updates. Obviously not a great solution for a small upload, but for those a progress bar probably isn't a big concern. If you can use another library there's an example here http://www.ibm.com/developerworks/library/os-php-v525/ – shaunhusain Apr 13 '12 at 15:54
  • @shaunhusain lol I already posted that link. Tom the javascript code is all you have to convert to AS3 the server side of it should remain the same. Basically you want to poll the progress page on the server to get the current status of the upload. Maybe this weekend I will create an uploader library and post it to google code. – The_asMan Apr 13 '12 at 16:30
  • I'm thinking about _faking_ the progress bar. Is there a way to predict the time that will be needed to upload the data? – Tom Apr 13 '12 at 16:54
  • @The_asMan Doh! :) okay maybe I need to take a break from SO seems you've got this one under control – shaunhusain Apr 13 '12 at 17:08

1 Answers1

0

You can add an event listener on scriptRequest for ProgressEvent.PROGRESS to monitor load completion. The event callback will contain bytesLoaded and bytesTotal to monitor.

ProgressEvent reference: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/ProgressEvent.html

scriptRequest.addEventListener(ProgressEvent.PROGRESS, onProgress);

function onProgress(e:ProgressEvent):void {
   trace(e.bytesLoaded + " of " + e.bytesTotal);
}
Jonathan Dunlap
  • 2,581
  • 3
  • 19
  • 22