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.data
contains 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);
}