I want to load a local .txt file and work with the content in javascript. My local file is like C:\Users\Who\Desktop\file.txt Thanks
To read a file, use FileReader , which enables you to read the content of a File object into memory. You can instruct FileReader to read a file as an array buffer, a data URL, or text.
There is a built-in Module or in-built library in NodeJs which handles all the writing operations called fs (File-System). It is basically a JavaScript program (fs. js) where function for writing operations is written. Import fs-module in the program and use functions to write text to files in the system.
Make sure you check the source of the document once it's loaded in the browser (all browsers let you do this, right-click "view page source" or similar). If you see the contents of version. txt anywhere in there, you're on the right track, you just need to move it into the body tag so that it will be rendered.
The possible ways to create and save files in Javascript are: Use a library called FileSaver – saveAs(new File(["CONTENT"], "demo. txt", {type: "text/plain;charset=utf-8"})); Create a blob object and offer a “save as”.
by default javascript is NOT allowed to access local file system for security reasons. If you want to allow a particular script access to a local file then you have 2 options.
1a. Change your model, put the text file on a server and load from there...
1b. Run a local webserver :-)
2 ... this becomes browser dependent,
In particular,
you can create a signed javascript for Mozilla like browsers, see http://www.mozilla.org/projects/security/components/signed-scripts.html for details
you can create an ActiveX plugin that allows local access for IE types... ;
and for anything else again read up o local access.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With