1

I've been trying to figure this out for several hours now to no avail. In javascript, I have string of XML that I've created:

var txt="<bookstore><book type='cooking'>";
txt+="<title>Everyday Italian</title>";
txt+="<author>Giada De Laurentiis</author>";
txt+="<year>2005</year>";
txt+="</book>";

I've then used this to create the XML dom:

if (window.DOMParser) {
    var parser=new DOMParser();
    var xmlDoc=parser.parseFromString(txt,"text/xml");
}
else{ // Internet Explorer
    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.loadXML(txt);
}

I need to send the xmlDoc to a server page (an asp page I think) where the server will prompt me to save the xml file to my local drive. I don't have too much experience with this, and have hit a road block. I know that I need to create an XMLHttpRequest and post my xmlDoc to an asp page:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://MYSERVERURL/xml.ASP", false);  
xmlhttp.send(xmlDoc); 

My problem is that I have no idea what should go into my xml.ASP page to receive the xmlDoc and prompt me to save to an xml file.

I have this so far, but really don't know where to go from here:

<%
response.ContentType="text/xml"
set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
xmldoc.async=false
xmldoc.load(request)
%> 

Any help is greatly appreciated.

Thanks.

1 Answers1

0

Not sure I understand what you want to achieve, but if all you want to do is force an Open/Save Dialog box to appear and allow saving of whatever was posted to the script then you can do this with this simple code:

<%
Response.ContentType = "application/x-unknown"
Response.Addheader "Content-Disposition", "attachment; filename=""mydoc.txt"""
Response.Write(Request.Form)
%>

Update: URL decode function.

function URLDecode(sText)
    sDecoded = Replace(sText, "+", " ")
    Set oRegExpr = Server.CreateObject("VBScript.RegExp")
    oRegExpr.Pattern = "%[0-9,A-F]{2}"
    oRegExpr.Global = True
    Set oMatchCollection = oRegExpr.Execute(sText)
    For Each oMatch In oMatchCollection
        sDecoded = Replace(sDecoded,oMatch.value,Chr(CInt("&H" & Right(oMatch.Value,2))))
    Next
    URLDecode = sDecoded
End function
johna
  • 10,540
  • 14
  • 47
  • 72
  • Hi John. Using this code, I was able to create a form that submits my string to the asp page and downloads it as a text. However, it's full of %2+ characters, and is missing my xml <> brackets. Is there any way to fix this? Thanks. – Benson Ryder Oct 17 '12 at 15:22
  • Apparently, the asp page uses percent encoding. Is there a way to turn this off so that I get the raw xml? I've added Response.CharSet = "utf-16" and Response.CodePage = 65001 but this doesn't seem to help. – Benson Ryder Oct 17 '12 at 19:27
  • Would URL decoding solve the problem? I have added a function to do this. Change output line to Response.Write(URLDecode(Request.Form)). – johna Oct 17 '12 at 20:41
  • It worked on everything but spaces, they still show up as + signs. How can I alter your regular expression to convert + signs to spaces? Thanks for your help. – Benson Ryder Oct 17 '12 at 21:25
  • Probably best accomplished with a simple replace command, I editted and changed the second line to sDecoded = Replace(sText, "+", " "). Not sure what will happen if you really want a plus symbol in your file though (they have hopefully been URL encoded). – johna Oct 17 '12 at 23:19