Forum Moderators: open
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
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.
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...
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.
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
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?
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();
}
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!