Question : Adobe Flex: Browse For XML File(FileReference), Modify It, then upload it to server?

the following code is to browse for local XML file, load it into my application as XML file or XML Document, then update it and after all I have to upload it to server


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
[b]private function browseForFile():void
[/b]{
	var xmlTypes:FileFilter = new FileFilter("XML (*.xml)", "*.xml");
	var xmlFile:Array = new Array(xmlTypes); 
	refUploadFile = new  FileReference();
	refUploadFile.browse(xmlFile);
	refUploadFile.addEventListener(Event.SELECT,onFileSelect);
	refUploadFile.addEventListener(Event.COMPLETE,onFileComplete);
}
	  		
private function onFileSelect(event:Event):void 
{
	trace("Selected");
	refUploadFile.removeEventListener(Event.SELECT,onFileSelect);
	refUploadFile.addEventListener(Event.COMPLETE,onFileComplete);
        refUploadFile.load();
}
     		
[b]private function onFileComplete(event:Event):void[/b]
{
	var newXML:XML = refUploadFile.data as XML; // [u]Here is the problem, how to convert the data I get to an XML file, using this way makes the newXML=NULL[/u][i][/i]
	refUploadFile.removeEventListener(Event.COMPLETE,onFileComplete);
        refUploadFile = event.currentTarget as FileReference;
}


Answer : Adobe Flex: Browse For XML File(FileReference), Modify It, then upload it to server?

FileReference.data 's type is ByteArray so here you'll have to convert the ByteArray to XML.

Possible solution:
------------------
var data:ByteArray = refUploadFile.data as ByteArray;
data.position = 0;
var newXML:XML = XML(data.readUTFBytes(data.length));
------------------------

Hope this will help.
Random Solutions  
 
programming4us programming4us