Kezern
Kezern

Reputation: 473

Read xml file from client

I'm trying to read a xml file from client. After search for it. I have got to upload the file to the server with this code.

var imagesFilter:FileFilter = new FileFilter("*.jpg,*.gif,*.png", "*.jpg;*.gif;*.png;*.jpeg");
fileRef.browse([imagesFilter]);

But what I want to do is to read the file from client and parse it without uploading it to the server. Can anybody help me? Thanks

Upvotes: 0

Views: 2088

Answers (4)

woo
woo

Reputation: 226

I wrote a simple flex app that ask for file, load it, and show file's content in textarea. Compiled with flex 4 SDK.

<?xml version="1.0" encoding="utf-8"?>

        public function onInit():void {
            m_FileRef = new FileReference();
            m_FileRef.addEventListener(Event.SELECT, onBrowse);
            m_FileRef.browse();

        }

        public function onBrowse(e:Event):void {
            m_FileRef.addEventListener(Event.COMPLETE, onComplete);
            m_FileRef.load();
        }

        public function onComplete(e:Event):void {
            contentArea.text = m_FileRef.data.toString();
        }           
    ]]>
</mx:Script>
<mx:TextArea id="contentArea" width="100%" height="100%">

</mx:TextArea>

Upvotes: 1

Mikhail
Mikhail

Reputation: 87

Code example:

private function onCreationComplete():void {
            fileRef.addEventListener(Event.SELECT, selectHandler);
            fileRef.addEventListener(Event.COMPLETE, completeHandler);
            fileRef.addEventListener(flash.events.IOErrorEvent.IO_ERROR, onIoError);
            fileRef.addEventListener(flash.events.DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);
        }

        private function selectHandler(event:Event):void {
           filename.text = fileRef.name;
        }


        private function selectFile():void {
            try
            {
                var success:Boolean = fileRef.browse();
            }
            catch (error:Error)
            {
                trace("Unable to browse for files.");
            }
        }

        private function onIoError(event:flash.events.IOErrorEvent):void{
            Alert.show(rm.getString('ui_res', 'file_uppload_fail'), rm.getString('ui_res', 'connection_error'));
            ModelLocator.getInstance().confManagerModel.isPending = false;
        }

        private function onUploadDataComplete(event:flash.events.DataEvent):void {
            trace("onUploadDataComplete");

        }

        private function responseHandler( event:DataEvent ) :void {
            var data:Object = JSON.decode(event.data as String);
            // do anything with data

        }

        private function uploadFile():void
        {
            if(!submit.enabled){
                return;
            }
            var request:URLRequest = new URLRequest("test")
            try
            {
                fileRef.upload(request);
                ModelLocator.getInstance().confManagerModel.isPending = true;
            }
            catch (error:Error)
            {
                Alert.show(rm.getString('ui_res', 'file_uppload_fail'), rm.getString('ui_res', 'error_on_server'));
            }
        }

        private function completeHandler(event:Event):void
        {

        }

Upvotes: 1

woo
woo

Reputation: 226

You can use method "load" and property "data" from FileReference class. I assume following workflow:

  1. user select a XML file via browse dialog
  2. You call a "load" method
  3. When load are complete, use "data" property(it's just an ByteArray)

Upvotes: 0

Mikhail
Mikhail

Reputation: 87

You can not read a local file from user machine, because "sandbox" of the web applocation has no access to the local files. Then your upload file to the server Flash player use standart browser API to do it. What information containts needed XML?

Upvotes: 0

Related Questions