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

Read and Write Files directly from the file system

Read and Write Files directly from the file system

iamt_ps
Member

Hello,

I want to ask if there is a way to directly read and write files (especially csv files) from the file system of the computer. It would also be interesting if I could only create new files or if there is a way to write to already existing files. I know that you can create files and download them in the browser but it would be better if the files could be written via a file path. Currently the WebIQ Server is running on the ctrlX Core but I could switch the Server to the PC if this is needed.

Thanks for the help! 

13 REPLIES 13

webiq-sk
Frequent Contributor

Please specify which computer you are talking about: the server or the client, i.e. user's browser.

The last thing is completely impossible with web technology for security reasons - imagine visiting a website and it would automatically read confidential files on your computer and transfer them to some remote country or place viruses on your disk. The only thing you can do is prompt the user to download a file like on any other website - and you cannot even preset a target path because for security reasons web technology does not allow you to do that either.

Likewise a user can only upload a file on a website and has to choose the path himself. No automated file transfers.

On the server this is possible and has been documented here before.

Please don't forget: a(n open) web HMI is simply nothing more than any other website, as such it is limited by the same security features as any other website on the internet.

Thanks for the Answer as said currently the Server is running on the ctrlX Core and the computer with the target file system is the client. I expected, that it does not work like this so I switch the Server to the computer so its the Server and the Client at the same time so it should work.

Is it possible to use the WebIQ Designer as a Server so I can develop and try the code that i need on my pc with its file system before I switch the Server from the ctrlX Core to the real target pc?

I have looked up the linked post and also found a dcoumentation on your website about the API Reference but in the post the example uses the command fm.save() while the documentation shows that the File I/O commands are fs. is there a difference or are they doing the same thing?

 

webiq-sk
Frequent Contributor

1. On ctrlX WebIQ does not have access to the root file system so you cannot access the ctrlX filesystem from a WebIQ HMI.

2. WebIQ Designer features a built-in webserver you can use for testing your HMI (it will reset the connection after 30 minutes and you have to manually reload the page in the browser as to prevent users from using a cheap Designer subscription in place of a more expensive one-time fee Runtime license).

3. It is named "fm" in the linked code, because the person that wrote the code named the variable "fm" and not "fs" - in JavaScript you can name it as you like:
webiqsk_0-1723792629362.png

 

webiq-sk
Frequent Contributor

Usually the most recommended question here is: what do you want to achieve? Usually saving files on the client system with the browser open - if it were possible - would not be recommended in general, because if the browser is not there or the connection was reset the files wouldn't be generated. If there would be multiple browsers open the data would probably be unnecessarily stored on all of them.

I'd rather recommend instead of trying to do things like these rather using backend functionality which allows you to do this cleanly - simply having some e.g. NodeJS program you write (it could in any programming language that supports websockets) that connects to the WebIQ Server API and the simply retrieves the information and writes the data as CSV files or whatever you like. As it would be running on the OS directly it can do anything any other program can do on such a system.

We also have an API Demo in NodeJS here: https://www.smart-hmi.com/user/download/deliver/xmp/Additional%20Software/webiq-server-api-example-n...

 

1. and 2. alright thank you then I will use the Designer as the Server until the Server is switched to the target computer. Thank you!

3. You are right I did not see that by the time I was writing the comment. I have tried the exact same code that is written in the post and put it in a UI Script that is triggered by a button but unfortunately it does not work for me. The Documentation of the API Reference doesnt really help either.

Is it possible that you could give me a simple working example how to read and write a .txt or .csv file so that i can figure the rest out from there?

Thank you for the Link but I can't open the HMI Project. If i try to import the project i get this error:

iamt_ps_0-1723798341919.png

But I managed to look in the JavaScript File of the project so I try to figure it out from there.

 

I got the Code from the other Post to finally work now. I did a mistake when implementing it to my project. Is there a way to use fm.save() and give it a custom file path so that I can save the file outside the project folder?

webiq-sk
Frequent Contributor

For security reasons you cannot create files outside of the project directory.

Okay thanks is it at least possible to create subfolders in the project directory and store the files there?

webiq-sk
Frequent Contributor

The CSV export is not and cannot be an HMI project so it cannot be loaded with WebIQ. As pointed out it is a completely independent NodeJS program that connects to the WebIQ Server API, retrieves the data and writes it to disk.

webiq-sk
Frequent Contributor

That should work (did you try?) but please note that in general these files will be removed when updating an HMI to a later version.

Once again I highly recommend using the clean version of a separate program, e.g. in NodeJS, that does all the work for you as it works even when no HMI is running in a browser, but started in the server.

Alright thank you!

Then I will use a separate Programm for the csv-files.

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