1

I have a scenario where the user clicks on a menu item in a flex application, that menu item is supposed to give them the file browser on their OS (Mac OSX for now) and then once they choose the file, I pass that file's local path along to another part of the application (that's already written) which makes a connection using that.

I've done some research and found code that can only be used if I include the AIR SDK which I prefer not to, is there a way to do this in actionscript or javascript (since I can call javascript from my actionscript application)?

Edit: the service I am passing the path to requires having the full local path which means that after browsing files, I have to get the local path to pass it along!

1 Answer 1

1

Without using AIR, you can still spawn a file browse dialog for uploading local files using the FileReference class with a Flex or pure ActionScript project.

This can be abstracted to a service class.

Application

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark">

    <fx:Script>
        <![CDATA[
            protected function buttonClickHandler(event:MouseEvent):void
            {
                var uploadService:UploadService = new UploadService("http://destination.com");
                uploadService.browse();
            }
        ]]>
    </fx:Script>

    <s:Button label="Upload..."
              click="buttonClickHandler(event)" />

</s:Application>

Upload Service

package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.FileFilter;
    import flash.net.FileReference;
    import flash.net.URLRequest;

    public class UploadService extends EventDispatcher
    {

        public var fileReference:FileReference;

        public var fileTypes:FileFilter;

        public var uri:String

        public function UploadService(uri:String)
        {
            this.uri = uri;
        }

        public function browse():void
        {
            fileTypes = new FileFilter("* (*.*)", "*.*;");
            var allTypes:Array = new Array(fileTypes);

            fileReference = new FileReference();

            fileReference.addEventListener(Event.SELECT, fileSelectHandler);
            fileReference.addEventListener(Event.OPEN, fileOpenHandler);
            fileReference.addEventListener(Event.COMPLETE, fileCompleteHandler);
            fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, fileSecurityErrorHandler);
            fileReference.addEventListener(IOErrorEvent.IO_ERROR, fileIoErrorHandler);

            fileReference.browse(allTypes);
        }

        protected function fileSelectHandler(event:Event):void
        {
            fileReference.upload(new URLRequest(uri));
        }

        protected function fileOpenHandler(event:Event):void
        {
            dispatchEvent(new Event(Event.OPEN));
        }

        protected function fileCompleteHandler(event:Event):void
        {
            fileReference.removeEventListener(Event.SELECT, fileSelectHandler);
            fileReference.removeEventListener(Event.OPEN, fileOpenHandler);
            fileReference.removeEventListener(Event.COMPLETE, fileCompleteHandler);
            fileReference.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, fileSecurityErrorHandler);
            fileReference.removeEventListener(IOErrorEvent.IO_ERROR, fileIoErrorHandler);

            dispatchEvent(new Event(Event.COMPLETE));
        }

        protected function fileSecurityErrorHandler(event:SecurityErrorEvent):void
        {
            dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR));
        }

        protected function fileIoErrorHandler(event:IOErrorEvent):void
        {
            dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
        }

    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way I can get the local path to that file chosen by the file reference? Once the file is selected, is there a way to figure out the local path? (Thanks for the help, I really appreciate it)
I believe only name and extension without path. This browse and upload functionality can be abstracted to other areas of your app; however, the other part of your app you've described could not read from the local file system by passing a path. So, if you have specific implementation such as socket communication, this won't help. You'll need an additional layer of your app / server to support upload.
Actually, the other part of my application requires the full local path (since I will be passing that to another service which I have no control over to implement an upload service in it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.