FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
08-16-2024 07:42 AM
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!
Solved! Go to Solution.
08-16-2024 08:12 AM
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.
08-16-2024 08:44 AM
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?
08-16-2024 09:59 AM - edited 08-16-2024 10:00 AM
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:
08-16-2024 10:06 AM
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...
08-16-2024 10:14 AM
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?
08-16-2024 10:24 AM
I suggest looking at this: https://www.smart-hmi.com/user/download/deliver/xmp/Additional%20Software/webiq-trend-export-to-csv-...
08-16-2024 10:53 AM
Thank you for the Link but I can't open the HMI Project. If i try to import the project i get this error:
But I managed to look in the JavaScript File of the project so I try to figure it out from there.
08-16-2024 11:34 AM
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?
08-16-2024 11:54 AM
For security reasons you cannot create files outside of the project directory.
08-16-2024 12:04 PM
Okay thanks is it at least possible to create subfolders in the project directory and store the files there?
08-19-2024 09:07 AM
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.
08-19-2024 09:08 AM
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.
08-19-2024 09:36 AM
Alright thank you!
Then I will use a separate Programm for the csv-files.