views:

543

answers:

4

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

A: 

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?

Mikhail
A: 

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)
woo
Hi woo. I can't find load method in FileReference class. I see it exist in the docuemnt APIhttp://livedocs.adobe.com/flex/3/langref/flash/net/FileReference.htmlBut when I try it in Flash Builder with Ctrl+space it does not show load as a valid method. Could you give me a small piece of code with browse and load method?Thanks for your patience.
Kezern
Sorry, I miss you question. I tried in Flex Builder 3 for flash 9, and it get me the same results. But then I change SDK version to flex 4 and target flash player to version 10. And there method "load" exist.Sorry again for late answer.
woo
Maybe another option can be: update your flex sdk 3 to latest version, if you don't want to use unstable flex 4
woo
+1  A: 

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
        {

        }
Mikhail
I don't know where the problem is. I have an application working that uploads the files. I have added the listener fileRef.addEventListener(flash.events.DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);and created this functionprivate function responseHandler( event:DataEvent ) :void { var data:Object = JSON.decode(event.data as String);}But despite of the file is uploaded correctly, this function is never called
Kezern
+1  A: 
woo
Thanks. finally I used a servlet and parsed the file into de servlet with XPath.
Kezern