How to Read a Local File Using Node.js - javascript

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.

Related

ReactJS - check the existence of local file using 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.

Read from file in Javascript

Hello I am developing a Google Chrome extension and need to read some information from a .txt file, so I did some research but i just found how to choose a file and read from it, but what I need to do is just read from a path, like c:\file.txt, not choose it and after read. I would really appreciate if any one could help me, thanks.
You should be able to use the runtime API to read local files if they are inside the app package directory using getPackageDirectoryEntry.
If you are trying to read random files then it's not possible without the user selecting it, js is sandboxed.

How do I read from external files in JS Bin?

I've been doing Code Year at Codecademy and I wanted to start practicing Javascript for myself, but I've been having a tough time figuring out some basic issues.
For my first project, I want to read in from a spreadsheet. I can't figure out how to access the data from its original source online, so I thought I would just save it as a text file. My question, then, is how to read from that.
So it looks like you can't read local files in Javascript. (Although apparently that's changing with HTML5? I don't have any familiarity with that.) So do I have to upload the text file someplace? Can I upload files to JS Bin? If not, does anyone have any recommendations for where I can upload the text file? And either way, once I do, what's the code to read from it?
Thanks in advance. I am sure this question is riddled with misstatements and improprieties, but I've spent a significant amount of time on this and I can't find anything that seems to answer my question. I honestly thought it would be something simple, like "var inputfile = c:\file.txt" but that seems not to be the case. I am totally lost. Thanks!
You can't. File system and storages in Javascript (or rather the client) is sandboxed.
That means you can only read what is written there in the first place. This has to do with security.
You will need to drop (or select) the local files into the browser and have some mechanism there to receive the drop/selection and store the file to one of the local storing mechanisms such as indexedDB or file API (the latter currently only supported in Chrome). For text files localStorage works fine too.
Resources:
http://updates.html5rocks.com/2011/08/Saving-generated-files-on-the-client-side
http://www.w3schools.com/html/html5_webstorage.asp
http://www.w3.org/TR/webstorage/
http://www.w3.org/TR/FileAPI/
http://www.w3.org/TR/IndexedDB/
The other option is to upload it to server and download it from there when you need it.
When you get to this point in your development, its time to run your own webserver for testing as it makes things much easier. If you must insist on doing it your way, uploading the file to a file hosting site and reading it in is still possible. Codecademy is great for getting started, but when you get into dealing with persistent data sources (either files or databases) its time to get web hosting or set up your own test server.
Even then you don't REALLY need your own test server, just a folder on your computer. You can access the files with File://, and link in the file you want to read as a relative path. If the .txt file is in the same directory, you just link it in as "Example.txt" when you open the html file in that directory.

Include HTML file via JavaScript

This is a file stored locally, not on a server, so Server Side Includes do not work.
Problem:
I have an HTML file. There is lots of data in it, I want to split it into smaller parts, and then just include them all into my big html file, i.e. something like:
main.html
<include "partA.html">
<include "partB.html">
<include "partC.html">
And I want the result as if the contents of partA,B,C.html were read right into main.html
Now, this is not on a server -- it's stored locally, so I can't do SSI. My question is:
Is there some simple way to do this via JavaScript? It seems like with JavaScript, I shoudl be able to:
fetch the contents of blah.html [not sure how to do this ste[
call a document.write on it, to write it into the document
probably handle some stuff dealing with escaping strings
Question:
How do I do this?
Thanks!
It's not possible, as a security feature. This post here is a discussion on the topic - Includes without local server?. As the answers say, your best best is to install a small web server on the machine if you can. They're not too hard to get going.
I have used nginx before with good results. http://en.wikipedia.org/wiki/Nginx
HTML5Rocks has a tutorial on how to read local files using HTML5's File API:
http://www.html5rocks.com/en/tutorials/file/dndfiles/
Outside this, Javascript generally does not have the ability to access local filesystems.
Update - I assumed the main file was on a server, and you wanted that file to access local files. On re-reading, it appears all your files are local, in which case, some of the answers below will work.

Needed help with javascript

So guys I am working on a program to like download stuff using javascript. I hav written the following code:
function download()
{
alert("Hello");
var url='http://somesite/somefile.rar';
window.open(url,'Download');
}
The code is pretty easy, but is there some other way to download the file using javascript? Also having downloaded the file is there some way to store it locally in the location of our choice, like d-drive? I had come across the javascript file api while searching the web, is it any useful in my scenario? Please help.
Edit: Fixed code formatting
No, this isn't possible. It is up to the client where to save files, not you.
Through Javascript, you can only set the file name that you want, but would not be able to access the file system i.e. All the folders, etc..

Categories

Resources