Can JavaScript read files on the hard drive? - javascript

Here I have learned that it is possible with JavaScript in HTML to create files offline and save them on the hard disk.
But what about reading files? Can JavaScript access, say one of my .txt files on my self phone and display the read data (or at least place it in the local storage)?
I know that it is possible with PHP, but there is no PHP on my cellphone, while HTML is always there.

You can use <input type="file"> and FileReader to read files stored at local filesystem, see File API; also How FileReader.readAsText in HTML5 File API works?.
If trying at Chromium or Chrome to read file: protocol launch the browser with --allow-file-access-from-files flag, without other Chrome instances running, or use a different --user-data-dir, see Read local XML with JS.

Related

How do I use local json in my browser extension?

I'm incredibly lost and new to browser extensions. I am making one related to dragons. Normally I would just use fs but this isn't an option since it's not node.
My browser extension folder has only three files: manifest.json, content-script.js, and dragons.json. I am making the extension in content-script.js and it's going swimmingly, but now I would like to read from and write to dragons.json. My extension will not be published or used for any other users, only me. How can I access this file? I am not trying to read the USER'S files, nor any external online files, just the files that are stored in the extension's folder.

Read content of a local file with chrome extension

I am developing an extension for the chrome browser. I use the KangoExtensions framework. The extension is written in JavaScript. I want to read the content of a file, which is located on the local filesystem. What is the easiest way to implement this?
If you already know the file's path in the filesystem you just need to add a permission to file://*in your manifest file and make a GET XMLHttpRequest to the url file://{filepath}.

How to read local file that in located in the same directory as html using javascript

I have an application that creates html files that containes a lot of data to draw. This files are alwayes browsed from local file system (no http protocol at all).
I have an idea to compress the data and than place it in a raw format in a separate .dat file that will be put in the same place as .html one.
It is not a problem to read and uncompress this .dat file when it is on a web server. But I don't now how to read it when both files .html and .dat are local.
Of course one can use browser file objects for it but in this case user will be alwayes asked to do a stupid action like to chose the the only file from an control.
But I don't now how to read it when both files .html and .dat are local.
I take it you mean from JavaScript within that HTML file. Depending on the browser, you can't except via the File API (which, as you've observed, requires that you have the user select the file in an input[type=file] element).
The way you'd want to do this would be via ajax (XMLHttpRequest), and that would work in some browsers, but not in others. Chrome, for instance, denies all attempts to use ajax with file:// URLs (e.g., in pages served from the file system), even from local pages, even when you're using a relative URL like thefile.dat. IE allows it (or at least, older versions of IE did; I haven't tried it lately).
So unfortunately, I think the only way you can do this is via the File API, with the attendant input[type=file].

Use an existing local file without the need to choose

I have developed a script for myself to read and process local text and csv files on my computer using recent Chrome or Firefox browsers with the filereader api. The script will work on the computer, even though it is not connected to the internet or a local webserver.
The reasoning behind this is to have a standalone text file interrogator, which will work on almost any computer, requiring only a browser to execute and display reasonably formatted output. similar to an awk type application.
I use the filereader api and it works well.
I do not properly understand the workings of the browser, so my question may be very stupid.
Is it possible that my script can somehow pass the filename to the script filereader api, without having to choose a file from input type=file field.
eg I have a file called addresses.csv and it resides in the same directory as my html/js file with the filereader api code.
I want to simulate the choosing of a file, without accessing my local directory and without using a webserver, but a dropdown box of predefined filenames would be even better.
No, for security reasons its not possible, because otherwise people could open arbitrary files on the computers of their visitors which is definitely not intended.
This specification also assumes that the primary user interaction is with the element of HTML forms [HTML], and that all files that are being read by FileReader objects have first been selected by the user.
See http://www.w3.org/TR/FileAPI/#security-discussion
No you cannot. Javascript cannot read your local filesystem without the use of a file input or drag and drop files (html5) as it would be vulnerable to malicious use.
There was a start of a filesystem api which Chrome has included. But you cannot read/write to just any place you want on the filesystem, you can only do so to a sandboxed area on the filesystem. Wither or not they will continue to have this in future versions of Chrome though I do not know.
It also appears the W3C itself has discontinued the file system api

read file and display content in javascript

I need to save some data in a file and when loading the page i want to read this file and add this data to the page. I don't know why but this assignment is useless so we can't use database nor server stuff, so i have to save and read from a local file.
I searched the net and found knew that it is not allowed to access files directly from javascript but using the FileAPI in HTML5 i can select a file to read.
So my question is how is this done because there are no resources about this, i simply need to
read a file and into a string.
Thanks,
For using the File API (HTML5) including reading/writing the content of local files and some sample source code see these links:
https://developer.mozilla.org/en/Using_files_from_web_applications
http://www.html5rocks.com/en/tutorials/file/dndfiles/
https://developer.mozilla.org/en/DOM/FileReader
http://www.w3.org/TR/file-writer-api/
http://www.html5rocks.com/en/tutorials/file/filesystem/
You should be aware that not all borwser/versions support all of the above AND that access to local filesystem depends on security settings...

Categories

Resources