Javascript/JQuery Local File System - javascript

I have a problem regarding creating an XML file via a web browser and being able to read/write from it.
Multiple browsers have issues with this and I cant seem to find a solution that works.
I am creating a CMS which displays XML data in fields. I then want to read the values of those input elements and then overwrite the XML file using Javascript or JQuery.
The XML file will be stored on a web server and the client PC's will access the CMS via a webpage on the server and then reading/writing to the XML.
The Code below gives you an idea of what IM trying to do:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FILENAME ="xml.xml"; //<---- In Current Directory on Web server.
var file = fso.CreateTextFile(FILENAME, true);
file.WriteLine('<?xml version="1.0" encoding="utf-8"?>\n');
file.WriteLine('<Seating_Plan>\n');
var table = document.getElementById("table_assoc");
var rCount = table.rows.length;
for (var i = 1; i < rCount; i++) {
var id = table.rows[i].cells[0].children[0].value;
var deptcode = table.rows[i].cells[1].children[0].value;
var name = table.rows[i].cells[2].children[0].value;
var role = table.rows[i].cells[3].children[0].value;
var desc = table.rows[i].cells[4].children[0].value;
var image = table.rows[i].cells[5].children[0].value;
var asdir = table.rows[i].cells[6].children[0].value;
file.WriteLine('<Person id="' + id + '"><Dept>' + deptcode + '</Dept><Name>' + name + '</Name><Description>' + desc + '</Description><Role>' + role + '</Role><Image><image href="' + image + '"/></Image><AssociateDir><AssociateDir href="' + asdir + '"/></AssociateDir></Person>');
}
file.WriteLine('</Seating_Plan>');
file.Close();
}
However, This works only when accessing the webpage on a local system rather than the Server eg. via: "file://C:..." rather than:
http://localhost/admin.html
What is the best way to do this?
Any help would be appreciated.
Many thanks!
Aj

I suspect you're misunderstanding something here:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FILENAME ="xml.xml"; //<---- In Current Directory on Web server.
The web browser can't access the server's file system. That ActiveX object may allow file system access on the current host, but it's not going to allow it on other hosts. If there are no errors and the browser is allowing the ActiveX object to interact with the local file system then I'll bet it's successfully creating the file on the local file system. (In whatever happens to be the current working directory for the browser process, since no directory was specified.)
To interact with files on the server's file system you'll need to use server-side code.

Related

Data logging issue

I have a web server set up from my arduino and the web page is loaded from it. I use ajax to print the data in the web page.
Similarly I need to store the data in a local pc as a text file. I was successful initially in storing a text file, but the issue is that the data dosent get appended in the same file, there are multiple files created per instance.
Can someone help me in solving this issue. I'm restricted to use only javascript.
client.println("document.getElementById(\"flux_values_1\").innerHTML = this.responseText;");
client.println("var textToSave =[this.responseText]");
client.println("var textToSaveAsBlob = new Blob([textToSave], {type:'text/plain'});");
client.println("var textToSaveAsURL = URL.createObjectURL(textToSaveAsBlob);");
client.println("var fileNameToSaveAs = \"as\";");
client.println("var downloadLink = document.createElement(\"a\");");
client.println("downloadLink.download = fileNameToSaveAs;");
client.println("downloadLink.href = textToSaveAsURL;");
client.println("document.body.appendChild(downloadLink);");
client.println("downloadLink.click();");
Actually you are downloading the file in browser, you cannot append data in a file via browser, you have to do this in backend. Browser will always create a new file and download a new file. This is expected behaviour and cannot be changed.

Access files from local machine

I need to access list of files in "D//logs" folder from my machine using javascript.
And then display them as hyperlink in a html page. I tried to acheive this using ActiveX, but accessing files is throwing error.
(Developing only for Internet explorer)
var fso = new ActiveXObject("Scripting.FileSystemObject");
var xx = fso.GetFolder("d:\\logs");
var yy = new Enumerator(xx.Files);
var zz = new Enumerator(yy.item()); // how to acess files inside folder

Dynamically create list items from local xml file

I'm trying to create a menu from an XML file. The HTML file that I want to create the menu in, is located my main project folder. This folder also contains an xml folder in which my xml file (fruitDB.xml) is located. I understood that there are several ways of loading XML files and that some ways only work online. Eventually the menu is used for an HTML5 mobile app (don't know if this is usefull information), build using Appcelerator.
I've read some sources but it's still not clear to me how I can load an XML file. I have the following code in my header tag:
<script type="text/javascript">
function init(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "xml/fruitDB.xml", false);
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send();
var xmlDoc = xmlhttp.responseXML;
var Fruits = xmlDoc[];
alert(Fruits);
for (var i = 0; i < Fruits.children.length; i++) {
alert("hi");
var Fruit = Fruits.children[i];
var Name = Fruits.getElementsByTagName("name");
var Thumb = Fruits.getElementsByTagName("image");
var list = document.getElementById("menuButtons");
var listEntry = document.createElement("LI");
listEntry.document.createTextNode(Name);
list.appendChild(listEntry);
}
}
</script>
What I try to do here is open the init(); function using , load the xml file, though I'm not sure if giving a path (like I'm doing) is correct. After the XML is loaded it should create new 's in my HTML file and give them name (and eventually an image) which are stored in the xml file until all items from the xml are placed as list items. Sorry for the long sentence :P.
At xmlhttp.send(); I recieved the following error in my console:
XMLHttpRequest cannot load file:///D:/folder/folder/folder/xml/fruitDB.xml. Received an invalid response. Origin 'null' is therefore not allowed access.
Does this mean that using XMLHttpRequest won't work on local files and if not what other way can I use in order to achieve my goal?
XML doesn't have great support in Titanium. Also XML is often a pain to work with..
Why not use a JSON payload / document instead!
Format the JSON so it is an array of fruit objects
Then you just parse the payload into a javascript object that comes back from the web-service or your local file something like this:
var fruits = JSON.parse(yourHTTPObj.responseData);
Now you can loop over the fruit objects and say:
if (fruits[i].type === 'Apple') { //do something };

XMLHttpRequest and save file?

I am looking at creating an application in QT Creator for the Ubuntu touch environment for managing EVE online information. Much like the eve neocom app for Iphone. I need a starting point and since this is going to be a read only app I need info.
So I need a way to download and save a file via javascript.
download an xml file from the specified site
https://api.eveonline.com/account/characters.xml.aspx?keyID=xxxxxx&vCode=xxxxxxxxx
and save it in a folder (same number as keyID) as characters.xml, so I can later read it offline.
the xml file on the server has this info
<eveapi version="2">
<currentTime>2013-06-29 05:37:02</currentTime>
<result>
<rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
<row name="nawm oloektan" characterID="xxxxxxxx" corporationName="Str8ngeBrew" corporationID="xxxxxxxx"/>
</rowset>
</result>
<cachedUntil>2013-06-29 06:34:02</cachedUntil>
</eveapi>
this is the function I have so far for loading/reloading the xml file from server
function RefreshChar(keyID, vCode) {
var baseurl = "https://api.eveonline.com"
var request = new XMLHttpRequest()
var code = "?keyID=" + keyID + "&vCode=" + vCode
request.open("GET", baseurl + "/account/characters.xml.aspx?keyID=" + code)
}
I need to save the xml file and be able to read it later on using qml (QT creator)

Find %LocalAppData% path and add new folder with JavaScript

I created a Windows 7 Gadget and I need to create a place on each user's computer to store the Settings.ini file (Created from my SettingsManager.js file). The application packaging team at my company recommends I use
%LOCALAPPDATA%\Microsoft\Windows Sidebar\
and then add
Gadgets\iMon.Gadget\
subfolders. This is so each user's settings are stored in a unique location and won't be changed by any other applications or gadgets.
Do I need to use something along the lines of
var fso = new ActiveXObject("Scripting.FileSystemObject");
var folder = fso.CreateFolder("path");
Any help on how to do this would be appreciated.
Update: I found how to get the %localappdata% path, but I still need to create the new folder. Here's what I've tried without success:
var wshshell = new ActiveXObject("wscript.shell");
var localAppData = wshshell.ExpandEnvironmentStrings("%localappdata%");
var filePath = localAppData + "\\Microsoft\\Windows Sidebar\\Gadgets\\iMon.Gadget";
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filePath = localAppData + "\\Microsoft\\Windows Sidebar\\Gadgets\\iMon.Gadget";
I'm not sure I understand what you're trying to do here. is iMon.Gadget the name of your gadget? If so, this folder is automatically created for you upon installation of the gadget when the .gadget archive is executed. All the gadget's files and folders will be unpacked to
%LOCALAPPDATA\Microsoft\Windows Sidebar\Gadgets\iMon.gadget\
In your code, you can then proceed to manipulate files within this folder (and it's subfolders). To get the full path, you use
var gadgetPath = System.Gadget.path;
For example:
var oFile,
gadgetPath = System.Gadget.path,
oFSO = ActiveXObject("Scripting.FileSystemObject");
oFile = oFSO.CreateTextFile(gadgetPath + "\\test.txt", true);
oFile.WriteLine("Test.");
oFile.Close();

Categories