Forum Moderators: open

Message Too Old, No Replies

XML/SVG - using postURL() and/orXMLHTTPRequest

help on these please...

         

bendzi

11:19 am on Apr 16, 2008 (gmt 0)

10+ Year Member



hi all
I am trying to save a serialized svg dom node to an external file on the same location.

I am using internet explorer so you may wonder why not just use posturl(), well thats because i just couldnt figure out how exactly to use it so i resorted to using your example - i am new to svg & javascript & stuff so hopefully you can understand! i did manage to implement a loading functionality using geturl() though which works just fine and loads xml data from the file but posturl() seems to be a headache.

so i have tried an example from [wiki.svg.org...] which uses xmlhttprequest to achieve this. heres the code i need looking at...

/**
* httpRequest
* factory used for creating XMLHttpRequest objects
*/
function httpRequest()
{
var xmlhttp;
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
if (!xmlhttp) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
if (!xmlhttp) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xmlhttp = false;
}
}
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}

/**
* postURL
* mimics postURL function (ASV) for browsers which support XMLHttpRequest
*/
if (typeof postURL=='undefined') {
postURL = function(url, data, callback, type, enc) {
var xmlhttp = new httpRequest();
if (xmlhttp) {
xmlhttp.open("POST", url, true);
if (enc && (enc == null ¦¦ enc == 'gzip' ¦¦ enc == 'deflate')) {
xmlhttp.setRequestHeader("Content-Encoding", enc);
}
if (type) {
xmlhttp.setRequestHeader("Content-Type", type);
} else {
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
callback({status:xmlhttp.status, content:xmlhttp.responseText, contentType:xmlhttp.getResponseHeader("Content-Type")});
}
}
xmlhttp.send(data)
} else {
alert("Sorry, your browser/SVG viewer neither supports postURL nor XMLHttpRequest!");
}
}
}

/*****this is my piece of code i wrote myself**/
function serializeNode(node, fileName) {
if (typeof printNode != 'undefined') {
var pathdata = printNode(node);
alert(pathdata);
}
else {
// handle browser problem here
alert("Browser issue, use MSIE!");
return '';
}
postURL( fileName, pathdata, callback );
function callback( urlRequestStatus){
if(urlRequestStatus.success) {
alert('Saved Successfully!');
} else {
alert('Saving has failed!');
alert(urlRequestStatus.content);
}
}
}

var filename = "http://localhost/C:/Documents%20and%20Settings/Compaq_Owner/Desktop/fyproj1/test.xml";

this is the filename (im sure theres a way of getting the current location & using that + filename, instead of hardcoding it as ive done here so it would work on any system, but i havent figured that out yet...any pointers would also be appreciated!)...

this is how its called later on..
<g id='save' onclick='serializeNode(canvas, filenametosave);' transform='translate(20 140)'>
...
...

It all seems to work fine up until the "callback" is reached i then get the "Saving has failed!" message & nothing actually saves to the file.

I dont know what im doing wrong, or not doing after rigorous troubleshooting, but im hoping someone would spot it in a quick second!

If theres a better way of doing this, i would appreciate if you suggest it!

also, urlRequestStatus.content is empty, if that means anything..

Thanks alot
ben

cmarshall

12:06 pm on Apr 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, bendzi!

To be honest, I'd need to spend a bit more time than I have available at the moment to help you out, but I'll lay odds it's a simple AJAX issue, and not an XML problem.

I'd try posting the same question over in the J&A Forum [webmasterworld.com], and see if there's someone there that might be able to help you more.

bendzi

2:10 pm on Apr 16, 2008 (gmt 0)

10+ Year Member



no problem, cmarshall thanks...

now after much hours of trying out stuff & more research, i discovered that using the xmlhttprequest was just extra overhead for internet explorer (only useful if using firefox & co) & using the post(url) method wasnt that complicated....

as i mentioned i'd already implemented a loading functionality using getURL() which loads data from a file sucessfully, like so:

//*********loading from file
function loadAndParseFile(fileName) {
if(typeof(DOMParser) == 'undefined') {
getURL(fileName, fileLoaded);
function fileLoaded(data) {
var string = '';
if(data.success) {
string = data.content;
} else {
alert('Loading has failed!');
}
var node = parseXML(string, document);
canvas.appendChild(node);
}
}
else {
//handle browser compatibility problem here
alert("Browser not supported! Internet Explorer works fine!");
return '';
}
}

this has always worked fine! i tried to implement postURL() in the same way to save serialised dom fragments to the same file like so:

//****************saving*********
function serializeNode(node, fileName) {
if (typeof printNode != 'undefined') {
pathdata = printNode(node);
alert(pathdata);
}
else {
// might want to handle problem here
alert("Browser incompatibility, ues MSIE!");
return '';
}
postURL( fileName, pathdata, callback );
function callback( urlRequestStatus){
if(urlRequestStatus.success) {
alert('Saved Successfully!' + pathdata);
} else {
alert('Saving has failed!' + urlRequestStatus.content);
}
}

this sems to work fine, i get the message "Saved Successfully!" and no errors...but when i check the file, nothing is actually saved to it!
This is surely confusing & i have no idea what else to do, ive tried all sorts to no avail...any hlep please!

anyone who knows a better or "correct" way of saving to an external file please suggest...

cmarshall

5:55 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wait a minute. I still haven't had enough time to dump your code into a test page, but I see your post [webmasterworld.com] sitting out there all alone in JS land...

When you say "external file," are you referring to one on the user's browser or system?

There's all kinds of security issues brought up by this. It is possible that the browser itself may interfere with this.

bendzi

6:36 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



yeah, my post's just sat out there by its lonely!lol...

neways, by "external file" i just mean a file on the users system (residing in the same folder/area where the svg file is loaded from...ignore any security issues for now, i just want to be able to implement the saving functionality...

like i said im able to access the file & load xml data from it using getURL(), but for some strange reason, i cant do the same using postURL(). it all seems to work fine & i get the "saved successfully" message meaning "urlRequestStatus.success" is true, but nothing actually gets written to the file! strange, or maybe just my lack of experience! either way i need help & im running out of time!

cheers

cmarshall

7:02 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm really surprised that works. Writing files to a user's system has to be the holy grail of malware writers.

bendzi

7:18 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



uh oh..i think ive been mistaken here...i dont want my aplication to write to a file on some random users system or nothing like that!

its more like i've created this application for personal use & im trying to access my own files on my own system!

hence the geturl() works, im just accessing a file on my system!

does that make any difference?

cmarshall

7:20 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It might, but I have no idea how to use it, as I've never even tried. I'll lay odds browser security will restrict that as well. You might want to have the server send up a file via a different content-type header.

bendzi

7:49 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



rite...im was hoping it wouldnt be a big thing seing as the geturl() easily works! oh well, i'll just hope someone whos done this b4 just magically appears on this forum & sorts me out!

thanks alot, cmarshall!

just by the way, i tried implementing this a different way as well but which got the browser asking me if i wanted to allow an activex script to run or sumn like that, which i agree to...it says its succesfully saved as well - but nothin actually gets written to the file yet again! here it is...

function save(node, fileName)
{
if (typeof printNode != 'undefined') {
pathdata = printNode(node);
alert(pathdata);
}
else {
// might want to handle problem here
alert("FOUL!");
return '';
}
var fso = new ActiveXObject("Scripting.FileSystemObject");
var ts = fso.CreateTextFile(fileName);
ts.Write(pathdata);
ts.Close();
}

httpwebwitch

10:38 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hey bendzi - if you're writing this app for personal use and want to access the file system, go check out AIR by Adobe.

Get the SDK. You can write your application in HTML, CSS, and Javascript, and it'll work just like a desktop app. Tailor made for your needs, mate. Start with the tutorial and make a "hello world" application. I tried it for the first time this week, and within 2 hours I was building a working file explorer that also sent stuff over HTTP and AJAX. Wicked!

bendzi

11:05 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



thanks alot.. will ahve a look at that