FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
07-03-2024 07:36 PM - last edited on 07-04-2024 08:55 AM by HmiGuide
Hi,
I am looking for a way to read a file that I saved previously through a javascript script in web iq.I saved a txt file with FileManager's save function as you can see in the attached image. But once I have written, how can I read?
Thanks
Solved! Go to Solution.
07-04-2024 08:16 AM - edited 07-04-2024 08:17 AM
Using its URL...
const fm = shmi.requires("visuals.session.FileManager");
fm.save("my-text.txt", "This is some example text written to the file.", (error) => {
if (error) {
console.error("Error saving to file:", error);
} else {
console.log("Data saved to file successfully.");
}
}, true);
This will save the file to '/my-text.txt': http://localhost:10123/{my-hmi-directory}/my-text.txt (Runtime) or http://localhost:10124/my-text.txt (Designer Preview Server).
07-05-2024 02:05 PM
While a browser can't write to the Web server file system, there is the function FileManager.save() which you found in the WebIQ documentation.
Reading a file from the Web server is a basic and common function of the browser, therefore you do not need a special WebIQ function, just use common js script functions. Here is an example with XMLHttpRequest():
(function () {
var actions = shmi.pkg("visuals.session.userActions"); //get reference to userActions object
actions["readFile"] = function (p) {
const sFileName = 'test.txt'
let xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", sFileName, false);
xmlhttp.send();
if (xmlhttp.status == 200) {
let sRead = xmlhttp.responseText
console.log("reading OK")
console.log(sRead)
} else {
console.log("reading failed status=" + xmlhttp.status)
}
};
}());
07-15-2024 03:40 PM
While the existance of fs.write() function is a security issue, because you or anybody else can overwrite WebIQ scripts and other files. This function might be removed in the future. There are 2 alternative option to save data:
07-15-2024 03:59 PM
"you or anybody else" -> by default only users in the "admin" group are able to do this.