Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Writing XML data using ASP.Net

Participant ,
Jun 11, 2010 Jun 11, 2010

I have successfully written my XML from flash to the server using php. But now I need to have it write to a Windows server using either asp or asp.net. Below is my code for PHP. Have searched allot of places and found how to write text files and lines inside text files. But I use a URL variable to write my XML file name out with xml data I created in Flash as3.

For simplicty I am using localhost... but really is set to my web domain.

On the address line I pass: Example

http://localhost/testing/saveClient.php?ClientFile=myTest.xml

PHP CODE

<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
$data = $GLOBALS["HTTP_RAW_POST_DATA"];

$myFileName = $_GET['ClientFile'];
chmod($myFileName, 0777);

$fp = fopen($myFileName, "w+");
fwrite($fp, $data);

fclose($fp);

if(!$fp){
  echo("PHP write error. Check permissions.");
}
else{
  echo("File Saved");
}
}
?>

FLASH SAMPLE

myURLFile = "http://localhost/testing/saveClient.php?ClientFile=" + ClientFileName;
trace("GLOBAL - writeToDataFile - myURLFile = ", myURLFile);
xmlURLReq = new URLRequest(myURLFile);
xmlSendLoad = new URLLoader();

xmlURLReq.data = userDataXML;
xmlURLReq.contentType = "text/xml";
xmlURLReq.method = URLRequestMethod.POST;

xmlSendLoad.addEventListener(Event.COMPLETE, onWriteData, false, 0, true);
xmlSendLoad.addEventListener(IOErrorEvent.IO_ERROR, onWriteDataIOError, false, 0, true);

xmlSendLoad.load(xmlURLReq);

Thought this would be easier... since I don't know asp that well I am finding it diffcult to save the information.

If anyone can point me to some examples I can take it from there.

HERE IS WHAT I HAVE FOR ASP.

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="utf-8" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Save Client File ASPX</title>
<style type="text/css">
body {
font: normal 12px Verdana, Geneva, sans-serif;
color: #666;
}
</style>

<script language="vb" runat="server">
Sub WriteTextStatic()
Dim id As String = Request.QueryString("ClientFile")
Dim objStreamWriter As StreamWriter
Dim strPath As String
strPath = MapPath(id)
objStreamWriter = File.CreateText(MapPath(id))
objStreamWriter.Close()
End Sub
</script>
</head>

<body>
ASPX Page
<% WriteTextStatic() %>
</body>
</html>

Thanks

TOPICS
ActionScript
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Deleted User
Jun 11, 2010 Jun 11, 2010

You're bound to run into permission issues that'll need addressing, but here is the gist of it:

1. I used C# rather than VB: my preference.

2. I used a handler (.ashx) rather than a form (.aspx): simpler

SaveClient.ashx

<%

@ WebHandler Language="C#" Class="SaveClient" %>

using

System;

using

System.Web;

using

System.Collections.Specialized;

using

System.IO;

using

System.Xml;

public

class SaveClient : IHttpHandler

{

     public void ProcessRequest (HttpContext context)

     {

          HttpRequest req = contex

...
Translate
Guest
Jun 11, 2010 Jun 11, 2010

You're bound to run into permission issues that'll need addressing, but here is the gist of it:

1. I used C# rather than VB: my preference.

2. I used a handler (.ashx) rather than a form (.aspx): simpler

SaveClient.ashx

<%

@ WebHandler Language="C#" Class="SaveClient" %>

using

System;

using

System.Web;

using

System.Collections.Specialized;

using

System.IO;

using

System.Xml;

public

class SaveClient : IHttpHandler

{

     public void ProcessRequest (HttpContext context)

     {

          HttpRequest req = context.Request;

          HttpResponse resp = context.Response;

          HttpServerUtility server = context.Server;

          resp.ContentType =

"text/plain";

          string id = req.QueryString["ClientFile"];

          if (id == null) {

               resp.Write(

"Missing 'ClientFile' parameter.");

               return;

          }

          // Read the post data from the stream

          // same as PHP $HTTP_RAW_POST_DATA

          StreamReader reader = new StreamReader(req.InputStream);

          string data = reader.ReadToEnd();

          // write the post data to an XmlDocument

          XmlDocument dom = new XmlDocument();

          try

          {

               dom.LoadXml(data);

          }

          catch

          {

               data =

null;

          }

          finally{}

          if (data == null)

          {

               resp.Write(

"Missing data.");

               return;

          }

          // NOTE: need to use a path with write permissions

          string strPath = context.Server.MapPath(id);

          StreamWriter objStreamWriter=null;

          try

          {

               objStreamWriter =

File.CreateText(strPath);

               objStreamWriter.Write(data);

               resp.Write(

"File Saved.");

          }

          catch

          {

               resp.Write(

"Write error. Check permissions.");

          }

          finally

          {

               if (objStreamWriter != null)

               {

                    objStreamWriter.Close();

               }

          }

     }

     public bool IsReusable {

          get {return false;}

     }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 11, 2010 Jun 11, 2010

Thanks Raymond... never heard of the ashx file type. But then I am not much up on asp VB or C#. I added your coding into a new file saved and ran it. Wrote the file just as I had done with php... GREAT NEWS!!! THANKS

Now I need to read through this carefully because my flash is stopping when the data is being written... still the file is being made with all the correct information. This is more complex then I had thought it to be.

Thanks so much for the code and help!

Jim

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 11, 2010 Jun 11, 2010
LATEST

You're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines