views:

296

answers:

2

Uploading a file, i want to have an XML response from server.

So, in php, i do:

header("Content-type:text/xml");
echo"<whatever/>"; // any 100% valid XML

in flash, i use FileReference class and FileReference.upload() method

var file:FileReference = new FileReference();
file.addEventListener(Event.SELECT, FileSelected);
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploaded);

...

function FileSelected(e:Event)
{
    var req:URLRequest=new URLRequest(Main.baseURL+"/upload.php");
    req.method="POST";
    try{file.upload(req,"userfile")}
    catch(err:Error){Main.err("Can't upload",err,e)}
}

function uploaded(e:DataEvent)
{
    var s:String=e.data;
    trace(":"+s+":"); //output :<whatever/>: as expected
    var xml:XML=new XML(s); // fails with Error #1088: The markup in the document following the root element must be well-formed.
}

Seems like DataEvent.datacontains invisible garbage, that prevents it from parsing... My current solution is to manually cut off first char (0xFEFF):

function uploaded(e:DataEvent)
{
    var s:String=e.data.slice(1); // if i manually cut it off - all works
    trace(s); 
    var xml:XML=new XML(s);
}
A: 

0xFEFF is zero width no-break space. Try removing that xml header from the php code.

Amarghosh
removed text/xml header, but the 0xFEFF remains, tried text/plain - same story
joox
I'm out of options - trim it for the time being - until someone comes with a solution.
Amarghosh
A: 

UTF BOM. Whatever is generating that file you're trying to upload should be configured not to add it.

Josh Davis