Local file access with JavaScript
If the user selects a file via <input type="file">
, you can read and process that file using the File API.
Reading or writing arbitrary files is not allowed by design. It's a violation of the sandbox. From Wikipedia -> Javascript -> Security:
JavaScript and the DOM provide the
potential for malicious authors to
deliver scripts to run on a client
computer via the web. Browser authors
contain this risk using two
restrictions. First, scripts run in a
sandbox in which they can only perform
web-related actions, not
general-purpose programming tasks like
creating files.
2016 UPDATE: Accessing the filesystem directly is possible via the Filesystem API, which is only supported by Chrome and Opera and may end up not being implemented by other browsers (with the exception of Edge). For details see Kevin's answer.
Can javascript access a filesystem?
Tiddlywiki has several methods of saving data, depending on which browser is used. As you could see in the source.
- If ActiveX is enabled, it uses Scripting.FileSystemObject.
- On Gecko-based browsers, it tries to use UniversalXPConnect.
- If Java is enabled, it uses the TiddlySaver Java applet.
- If Java LiveConnect is enabled, it tries to use Java's file classes.
Can Javascript access files on the server?
The question you refer do doesn't use JavaScript to access the SQL database.
It uses JavaScript to access an ActiveX Control (which requires Internet Explorer + Widows + Heightened Security Privileges).
You can access the file system in the same way - given a suitable ActiveX control.
Access all files within a given folder (The File System Access API)
For future reference, I'm posting my work; https://github.com/akshayknz/filesystem-access-api/blob/main/file.html (A html page which displays all images from picked folder.)
Note:The API only work in secure contexts (i.e. it works in https:// and file:///)
[fileHandle] = await window.showOpenFilePicker();
const file = await fileHandle.getFile();
const contents = await file.text();
or
const dirHandle = await window.showDirectoryPicker();
const fileHandle = await dirHandle.getFileHandle(entry.name, {});
const file = await fileHandle.getFile();
Related Topics
How to Reload Page Every 5 Seconds
How Can a JavaScript Object Refer to Values in Itself
JavaScript Efficiency: 'For' VS 'Foreach'
Modifying Location.Hash Without Page Scrolling
Execute JavaScript Using Selenium Webdriver in C#
Is There an Alternative Method to Use Onbeforeunload in Mobile Safari
Cannot Use Attr with an Object in D3 V4
JavaScript Promises - Reject VS. Throw
Class VS. Static Method in JavaScript
Spread Syntax VS Rest Parameter in Es2015/Es6
Jquery Parallax Scrolling Effect - Multi Directional
Jquery Select Pseudo-Element :After
Add CSS Styled Marker to Google Maps
How to Detect CSS Flex Wrap Event