How to read saved file with WebIQ and Js?

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

Best reply by HmiGuide

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)
        }
    };
}());

 

View original
4 replies