FileReference: Loading a Windows-locked file
Asked Answered
P

1

7

I'm using Flex in Flash Player 10 on Windows, using FileReference to load a file into memory, as below.

My issue is that when a file is locked by Windows, my FileReference is not giving me any feedback that the file is inaccessible--it simply never dispatches any events after my calling load().

Does anyone have insight into how to tell that Flash Player is unable to open the file?

var fileReference:FileReference = new FileReference();

private function onClick():void {
    fileReference = new FileReference();
    fileReference.addEventListener(Event.SELECT, onSelect);
    fileReference.addEventListener(Event.COMPLETE, onComplete);

    fileReference.addEventListener(Event.CANCEL, onOther);
    fileReference.addEventListener(IOErrorEvent.IO_ERROR, onOther);
    fileReference.addEventListener(ProgressEvent.PROGRESS, onOther);
    fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onOther);
     // I've tried adding all of the other declared events 
     // for FileReference here as well

    fileReference.browse();
}

private function onSelect(event:Event):void {
    trace(fileReference.name);

    try {
        fileReference.load();
    } catch (e:Error) {
        trace(e);
    }
}

private function onComplete(event:Event):void {
    trace(fileReference.data.length);
}

private function onOther(event:Event):void {
    trace("other:" + event.toString());
}
Plantagenet answered 8/3, 2010 at 20:28 Comment(1)
I hope you get an answer. I'm curious about this one myself.Warranty
N
2

A possible (dirty) workaround might be to wait for -let say- 10 seconds, and suppose that the file isn't available if no event has triggered then.

Using a setTimeout (and clearing it with clearTimeout in your COMPLETE and *_ERROR events handlers) might do the trick.

I'll be glad if someone could come up with a nicer solution, though.


EDIT: Of course you might want to listen to HTTP_STATUS event (waiting for a 202 answer - if I understood this documentation correctly) rather than waiting for COMPLETE or *_ERROR.

Newfeld answered 12/3, 2010 at 13:46 Comment(3)
Would've been easier if you could access the win32 api, using CreateFile API to open the file for read access and check wether it returns an valid handler or not. But without that... your workaround sounds the only way to go.Calfskin
This is the way I ended up handling it--extending FileReference with an internal Timer instance that is started on load, stopped on "completion" (complete or error), and reset on progress. If the timer runs out, the load is cancelled and a timeout event is dispatched.Plantagenet
(I didn't use the HTTP_STATUS event because I'm only interested in files from the local machine; no HTTP events appear to be fired in that case.)Plantagenet

© 2022 - 2024 — McMap. All rights reserved.