Flash to ASP.Net C#
Trying to post XML to an ASP.NET C# page and then write that XML as a file.
- Errors I kept receiving
- "Root element is missing"
- Before checking for the error - when trying to load the Request.InputStream, if I turned off the load and looked at what was recieved, I got "System.Web.HttpInputStream"
- Final Solutions
- Request.InputStream.Position = 0;
ACTIONSCRIPT CS3
var respTxt:TextField = new TextField();
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.size = 14;
var xmlString:String = "<?xml version=\"1.0\" ?><root>Content passed from flash</root>";
var sampleXml:XML = new XML(xmlString);
var xmlResponse:XML;
var xmlURLReq:URLRequest = new URLRequest("http://tonerpackaging.orbisdesign.net/include/xmlWrite.aspx");
xmlURLReq.data = sampleXml;
xmlURLReq.contentType = "text/xml";
xmlURLReq.method = URLRequestMethod.POST;
var xmlSendLoad:URLLoader = new URLLoader();
xmlSendLoad.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
xmlSendLoad.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
xmlSendLoad.load(xmlURLReq);
function onComplete(evt:Event):void {
try {
xmlResponse = new XML(evt.target.data);
respTxt.text = xmlResponse;
removeEventListener(Event.COMPLETE, onComplete);
removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
} catch (err:TypeError) {
respTxt.text = "An error occured when communicating with server:\n" + err.message;
}
placeText();
}
function onIOError(evt:IOErrorEvent):void {
respTxt.text = "An error occurred when attempting to load the XML.\n" + evt.text;
placeText();
}
function placeText():void {
respTxt.x = 15;
respTxt.y = 15;
respTxt.multiline = true;
respTxt.autoSize = TextFieldAutoSize.LEFT;
respTxt.setTextFormat(myTextFormat);
addChild(respTxt);
}
C#
protected void Page_Load(object sender, EventArgs e)
{
DateTime CurrTime = DateTime.Now;
string rawXML="";
if(Request.InputStream != null && Request.InputStream.Length > 0){
XmlDocument xmlDoc = new XmlDocument();
try{
XmlTextReader readXmlFromFlash = null;
readXmlFromFlash = new XmlTextReader(Request.InputStream);
Request.InputStream.Position = 0;
xmlDoc.Load(Request.InputStream);
rawXML = xmlDoc.InnerXml;
MailMessage msg1 = new MailMessage();
msg1.From = "asdf@asdf.com";
msg1.To = "asdf@asdf.com";
msg1.Subject = "xmlWrite: " + CurrTime.ToString();
msg1.Body = "Its good: " + rawXML;
msg1.BodyFormat = MailFormat.Html;
msg1.Priority = MailPriority.Normal;
SmtpMail.Send(msg1);
string filename = Server.MapPath("/flash/xml/current.xml");
xmlDoc.Save(filename);
Response.Write("<?xml version=\"1.0\" encoding=\"iso-utf-8\"?><root><value>" + CurrTime + " - " + rawXML + "</value></root>");
}catch(Exception ex){
MailMessage msg1 = new MailMessage();
msg1.From = "asdf@asdf.com";
msg1.To = "asdf@asdf.com";
msg1.Subject = "xmlWrite: " + CurrTime.ToString();
msg1.Body = "Error during XML read: " + ex.Message;
msg1.BodyFormat = MailFormat.Html;
msg1.Priority = MailPriority.Normal;
SmtpMail.Send(msg1);
errorMsg = ex.Message;
}
}else{
Response.Write("Nothing passed");
}
}