ReactJS - check the existence of local file using Javascript - javascript

I need to check a particular software (built by us) has been installed to the Operating system. If not need to show a proper alert message.
As an alternative solution I'm going to do something like this
After installing the software there will be file in a given path (ex: c:\mysoftware\config.txt)
If there is any way to to check the existence of this config.txt file from Javascript then I can create a function like isSoftwareInstalled() in my reactjs application.
Am i doing right thing? or is there any professional but simple way of doing this?

As far as I understand, you must be keeping this file somewhere on the server.
If you are using Node/Express in the backend, you can use the Node File System readFile method.
https://nodejs.dev/learn/reading-files-with-nodejs
If you use some other backend language, kindly use File API in that.
In case, you are wondering about client-side file read, this is not possible with JS.

Related

How to Read a Local File Using Node.js

I have a relatively simple task that I want to achieve, but I am totally stumped on how to achieve it. I want to create an HTML page that allows users to select a local JSON file. I want to pass the path to this local file to some Javascript code. Then I want to read that file using Javascript every time the file changes.
I've investigated a few different methods for achieving this. First, I tried just using Javascript. This worked great until I wanted to read the file multiple times as it was changing. Evidently there are security concerns that make this impossible.
Next, I investigated using Node.js to solve this problem. It has some file utilities that seem perfect, and it's server-side, which may help with the security issues. But using just Node, I couldn't figure out how to select a local file in HTML and pass the path to the Javascript code. From investigation online, it seems like Express.js would be useful.
So now I'm trying to use Node.js and Express.js. Now I can figure out the file name from an HTML form! But I can't figure out the path to the local file so that I can read it using the Node file utilities... From looking at similar questions online, it looks like everyone uploads files, then reads them. Is that what I have to do with Express? If so, is it possible for me to monitor changes to the original file, upload the file when those changes get made, then read the changed file? Is what I'm trying to do possible? If so, does anyone have any recommendations on libraries/modules to use? Thank you so much for any help - please let me know if I can be more clear about anything.

How do I fetch and save files using JavaScript?

My goal is to create a program on a webpage. The program will Fetch an assembly folder. Then extract CNC files from the assembly folder and put the CNC files into one folder and sort the CNC files into 3 other folders (plywood, aluminum, or steel) based upon the filament type metadata in the CNC files.
What is the best method to doing this using JavaScript?
Is there a way to detect file properties such as whether a file is a CNC using JavaScript?
I was also thinking of incorporating Python, since JavaScript is too simple a language for this type of process. Is there a way to link JavaScript and Python, such that JavaScript will be the main class and Python will be used as a subclass(es)?
Thank you!
In, short you can't. It is not possible to do this using JavaScript in the browser. It is entirely possible to do this using Node.js. You can also certainly call Python from JavaScript and vice versa in Node. So to answer your questions:
The best method is using Node.js if you want to use JavaScript.
It depends on what you mean by "file properties". If you mean the EXIF data, then you can use a package called exif-parser or something similar. Maybe you want the MIME type of the file, if so then use mime-types. There's bound to be a package that you can utilize to achieve your needs.
It is possible to run JavaScript from Python and vice versa, but why? Both languages are more than capable on their own.

Reading configuration file(javascript file or json file) on the fly in Qml and javascript

I am new to Qt/Qml and javascript.
Let's say I have a javascript file which contains a table of parameters. The javascript file is used in Qml and it is located in the application directory. e.g. C:\Program Files\QtApp\the_table.js.
If end customer installed the Qt app, they want to manually edit the javascript file and use it with the installed application. ( No need to recompile the app source code).
How can I make it work?
I have tried to update the javascript file manually, but when I try to open the app again, the "updated entry" in the javascript file is not displayed in the application.
-----------------8<-----------------------
Updates:
Javascript file included in the qml resource will not be editable by
end user without recompiling the app.
Javascript in qml will hold mostly the logic, not the data.
It's better to use the JSON file to store the configuration data that can be modified by the end user.
Qt 5.0 and newer version has support for reading JSON file. QJsonDocument.
It sounds like the flexibility you require should be implemented at application level rather than externally. If it is just a "table of parameters" there are plenty of ways to do it in JS.
Due to implementation details you most likely don't want to bother with, that would be much preferable to updating the actual JS file.
Consider the usage of import Qt.labs.settings 1.0 which will allow you to store and make changes to settings that persist across application runs. That would be marginally easier than parsing config files. That being said, doing so is very much a viable option, you just have to interface QDataStream to QML.
That's the best I can do without knowledge of specific requirements and usage scenario.
OK, after a quick test, it seems that it is possible to import a file from the file system, you just need to:
import "file:///pathToYourExecutable/YourScript.js" as YourScript
Then you can use YourScript in QML. However, I would not recommend using this, as it invites opportunities to mess up with the application. The user shouldn't really be editing any files to make changes, just seems like bad design. Whatever it is that you want to achieve, there is most likely a better way to do it.
Solved by changing to use json file to store the data and then access the json file in QML.
The class to read the json file from the disk is JsonFile class in Qt, check this link:
https://forum.qt.io/topic/39756/qml-and-js-reading-json-file-and-change-content-on-the-go

Is there a way to read a local netcdf file from a web application?

We are building a web service for data analysis and would like to access netcdf files from the local machine where the browser is running. Javascript offers a file browser, but (for security reasons as I learned) it will automatically upload a file after selection, instead of allowing (read-only) access to it. This presents a show-stopper, because the netcdf files can be HUGE. Note, that the netcdf format and API explicitly allow slicing and extraction of individual variables, which is one reason why the format is so popular.
Now, some research into this issue revealed that the server-client architecture normally doesn't allow access to the local file structure to prevent spying. On the other hand, in HTML5 there is a file API which supports exactly the kind of operations we need -- except that you can access portions of a file by specifying byte ranges, but there is no netcdf API available; hence one would be left again with copying the entire file before being able to slice it on the server.
Of course, the other option are web services such as OpenDAP which are meant to do exactly what we want, i.e. access parts of a netcdf file over the internet. However, this of course requires that every user would have to install the OpenDAP server before they could access their local files from within the web service. (Or, at a minimum they would have to install a web server so that one could access the file via http://localhost/...).
So: does anyone know of a solution to read specific portions of a local netcdf file from a web application? Specifically, are there javascript tools available for this?
Currently you can use netcdfjs, that it's a javascript library that allows to read NetCDF v3 files. Because it's a NodeJS package you can run it server side, and for regular size files you can read it online, here you have an example.
There are no Javascript tools to read NetCDF, but there are Java libraries: http://www.unidata.ucar.edu/downloads/netcdf/index.jsp.
You could deploy a Java Applet to read the NetCDF file locally. From there you could have the Applet try to process the file, or communicate with a backend service, or even try to call on the Applet from Javascript: http://docs.oracle.com/javase/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html.
There are no specific Javascript libs for reading NetCDF, as far as I know. But let me drop some thoughts:
As you say, you can install a local webserver, and use a server-side language to execute a program that reads the file in your disk. You could use a command-line software called ncdump-json. I intentionally wrote this software for that purpose.
You say that you don't want to install software like OpenDAP or webservers, but maybe a desktop app, a standalone .exe would be OK for you. Using node.js plus chromium (this is called nw.js) you have a program that is at the same time a nodejs server, and a chromium browser. This way you can write a web application that reads the data (e.g. using the embedded nodejs server and spawning ncdump-json...).
And now the simplest scenario, a 100% client-side solution... I think this can only be achieved using Javascript... or anything that can "compile to" Javascript. We need to have the netCDF libraries in Javascript, is that possible? I guess so. Emscripten is a LLVM-to-Javascript compiler, and netCDF is C so it has to be possible to compile it with clang/LLVM, therefore it has to be possible to use Emscripten to have a netCDF JS library version with almost no effort, without having to write from scratch (and also maintain) a JS port. If I'm not wrong, to random access the files there's the method slice in the html5 file api, so that should not be a problem.
Hope I helped.

How to read local (Client) file with GWT?

I would like to use GWT to read and parse local csv file.
EDIT: Local file here means client file.
I know I can use HTML5 with javascript to achieve that, but I would like to use GWT to do it.
One way of doing it is by using JavaScript Native Interface (JSNI), however I think it beats the purpose of using GWT.
Another way of doing this would be upload the file to server, and send it back as a json. But I feel it's not the best way of doing it.
Please advise. Thanks.
EDIT: I intend to design a desktop app, so I try to avoid using any web-server scripting.
Using jsni doesn't beat the purpose of gwt, sometimes you need to use it as a wrapper to functionality not yet available in gwt or an external javascript library. Instead of writting jsni access to the file api you can use one of several third party libraries available that implement html5 file access, like http://code.google.com/p/lib-gwt-file
A quick search returned this SO question: Read text file in google GWT?
Although GWT doesn't support java.io.InputStream, it looks like you could make a request to your CSV File and then parse it.
Remember that if you will host on AppEngine, you won't have a filesystem available so you'll have to have the files hosted elsewhere.

Categories

Resources