BerkeleyBlue
BerkeleyBlue

Reputation: 19

How can I read a local file with a fixed path in Javascript? I only have the location of the file as a string. NO HTML forms allowed

I am creating a web app where I already know the location of a file that needs to be uploaded. Note that all I have is the path (example: Users/username/Downloads or Users/username/Documents) or something like that. How can I get Javascript to read that file as a File object or as a binary string so that I can POST it to a server.

Additionally, this file is not a simple file like a JSON or a txt file. This file would usually be a Microsoft Word file.

Upvotes: 0

Views: 246

Answers (2)

Shane
Shane

Reputation: 1

I think you can't to read the any files from the front-end by javascript, It is not allowed to do so. But you can get the file path from front-end and then pass it to the app framwork or back-end to processing. Beacuse the app's framework or back-end has the ability to access system files. Whereafter, you can POST to the contents of the file to the web server.

Upvotes: 0

Leftium
Leftium

Reputation: 17903

Due to security reasons, JavaScript executed from web apps have very limited access to the file system.

The best you can do is have the user select a file via a file input.

If having the web app user install a browser extension is acceptable, a browser extension may have more access to the user's file system. Here is an example extension that accesses a user's file system more directly: https://github.com/buggyj/savetiddlers

Upvotes: 3

Related Questions