2

I am using navigateToURL for file downloading from server. Is there anyway possible to know when navigateToURL has finished, more like when browser download dialog has opened?

Sometimes it takes 5 seconds to complete, user might get confused and start clicking download button like psychopath, which can result in multiple download dialogs opened.

I want to add some "please wait" text or something before it finishes (I already have one, I just need to know when to stop).
Maybe it can be done using javascript and get info from ExternalInterface?

randomUser56789
  • 936
  • 2
  • 13
  • 32

3 Answers3

1

This is kind of crazy, but I can't think of any other way: you could put the request object into a dictionary which is set to weak reference the keys, and then check on intervals whether the key was removed.

However, I'm not sure what will happen first, either the SWF itself will be disposed or the dictionary will be cleaned. It's also possible that given the one-time-ness of the function the reference to the request object isn't deleted because it is assumed to be deleted together with the whole SWF.

One more thing that I know is that uncaught error events will catch events from navigateToURL - not really helpful, but at least may give you the indication if it didn't work.

One more simple thing I can think of - just disable the button for a short time, like 1-2 seconds. If it worked, no one will see the delay, and if it didn't, they won't be able to press it too often.

private var _requestStore:Dictionary = new Dictionary(true);
private var _timer:Timer = new Timer(10);
. . .
_timer.addEventListener(TimerEvent.TIMER, timerHandler);
. . .
public function openURL(url:String):void
{
    var request:URLRequest = new URLRequest(url);
    _requestStore[request] = true;
    _timer.start();
    navigateToURL(request);
}

private function timerHandler(event:TimerEvent):void
{
    var found:Boolean;
    for (var o:Object in _requestStore)
    {
         found = true;
         break;
    }
    if (!found) // the request got disposed
}
  • How would the code know when to remove the key? URLRequest can't dispatch events as it doesn't extend EventDispatcher. uncaught error events are fine for uncaught errors; but once the request is sent off to the browser, I do not believe an uncaughtError will dispatch in a Flex app. – JeffryHouser Jun 11 '12 at 19:34
  • I'm not understanding what your dictionary approach will accomplish. Sending the request is very different than completing the request; if every request was finished immediately; the original poster wouldn't have a basis for a question. – JeffryHouser Jun 11 '12 at 20:08
  • Re-reading the original question, I think your interpretation is correct. – JeffryHouser Jun 12 '12 at 14:17
  • @wvxvw Hmm, I tried using dictionary with `WeakKey` putting my request object, but it doesn't seem to work for me, or maybe I am doing something wrong, could you give a little example or something? – randomUser56789 Jun 14 '12 at 07:03
  • Pretty much same as what I tried. Unfortunately urlRequest object gets disposed instantly. When timer function gets called, dictionary is already empty even if download dialog still hasn't been open. – randomUser56789 Jun 14 '12 at 09:44
0

Is there anyway possible to know when navigateToURL has finished, more like when browser download dialog has opened?

No, there is not. Once you pass a request onto the browser; the Flash Player no longer has any control or access to it.

You mention using ExternalInterface as part of a possible solution, but how would your HTML/JavaScript page know that a download had been finished?

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • That's what was afraid of. About JS, I don't know, not an expert in that field, but maybe there is some way to know when file is ready for download (browser standard download dialog opened). I'll try googling later. – randomUser56789 Jun 12 '12 at 06:13
0

navigateToURL does not fire a complete event. Try using this event from Adobe's help documentation:

public function URLRequestExample() {
        loader = new URLLoader();
        configureListeners(loader);

        var request:URLRequest = new URLRequest("XMLFile.xml");
        try {
            loader.load(request);
        } catch (error:Error) {
            trace("Unable to load requested document.");
        }
    }

    private function configureListeners(dispatcher:IEventDispatcher):void {
        dispatcher.addEventListener(Event.COMPLETE, completeHandler);
        dispatcher.addEventListener(Event.OPEN, openHandler);
        dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    }

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html#includeExamplesSummary

Dom
  • 2,569
  • 1
  • 18
  • 28
  • 1
    This may work if the original poster intends to download something into the Flash Application; but not if he intends the user to download it to their own machine. – JeffryHouser Jun 11 '12 at 19:41
  • Yeah, I was thinking that you could download the content to the flash app then serve it out to the user using the above. Not the most efficient way but it would allow for the complete event to be incorporated. Would appreciate your thoughts on that Flextras. – Dom Jun 11 '12 at 20:51
  • I guess, in theory, once you have the file in the Flex app you could use FileReference.save; but the user will have to click another button for this to work after the download is complete. The app will not be able to initialize a 'save' without the user interaction. – JeffryHouser Jun 11 '12 at 21:10
  • Ahhh yes, forgot about the user interaction requirement. Damn, thought I had something there. Thanks for the response Flextras. Be sure to nominate yourself to be a moderator. – Dom Jun 11 '12 at 21:12
  • Sorry, but FileReference is not an option for me. It doesn't have "OPEN" button. Another user interaction to initiate `save()` method isn't such a big deal for me. – randomUser56789 Jun 12 '12 at 06:07