cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

How to read saved file with WebIQ and Js?

How to read saved file with WebIQ and Js?

davifas
Member

Hi,

I am looking for a way to read a file that I saved previously through a javascript script in web iq.HmiGuide_0-1720076086994.pngI 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

4 REPLIES 4

webiq-sk
Frequent Contributor

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).

 

HmiGuide
Community Moderator
Community Moderator

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

 

HmiGuide
Community Moderator
Community Moderator

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:

  1. Make the WebIQ variable(s) you want to store, Persistent
  2. Store the data via JS in a WebIQ recipe. The WebIQ API provides all function to do this. From creating a recipe template up to save data and read data from a recipe.

webiq-sk
Frequent Contributor

"you or anybody else" -> by default only users in the "admin" group are able to do this.

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist