How can I get the actual file path of the selected file in Chrome instead of the 'fakepath'? [duplicate]
Asked Answered
R

1

6

After selecting a file in Chrome I need to get the full client side path to that file. Currently, the only path I get is something similar to:

C:\fakepath\some_file

All my googling lead me to the following answer: "you can't because of HTML5 security spec. Besides, why would you need to? You can upload it just fine without knowing the path."

This is a perfectly fine and valid answer except that I don't need to upload the file. The website I am managing can have any number of administrator users that configure the website by browsing for and storing paths to a large number of files. All these files are stored on shared network locations that the web server has access to.

The whole point of this is to eliminate upload time, to prevent the need to re-upload the files if the admins decides to change them and to keep the web server storage space to a minimum. Obviously, the web server also processes and manipulates these files. These are all client requirements.

The web server and the shared locations are all on an internal network not connected to the internet. Also, only the users with admin role have access to said locations and to the web pages that allow them to configure these paths. Security wise, this is enough for the client.

We've been using Internet Explorer and by adding the website to the trusted website list it worked just fine but now users are switching to Chrome, the new official and approved company browser. Internet explorer will soon be phased out.

Currently, users are copying and pasting the path into the input field but they want to be able to browse for the file as they did before.

How can I force Chrome to show me the actual file path, is there any equivalent to the trusted websites list from IE or how would you adapt the website to work with these conditions?

Rosalynrosalynd answered 28/9, 2018 at 7:20 Comment(6)
Since you said the web server itself has access to the drives, the website itself could provide a kind of JS file explorer. This would be a major change to the website of course and specific to your use-case.Spatterdash
Yes, I did think of this but, as you said, major change. I don't want to reinvent the wheel if I don't have to.Rosalynrosalynd
It seems though that the security restrictions are clear in this case. And the trend is that browsers tend to become more strict over time. Making that change will not only prepare you for that in the future, but also make the site browser-independent.Spatterdash
Good point. If this turns out to be my only option, you can submit this as an answer and I'll mark it as such.Rosalynrosalynd
Ask the user for the path, not the file.Conditional
I guess the user is literally too lazy. As I've specified in my question, the user can copy paste the path without any issues. The users explicitly want a browser file window.Rosalynrosalynd
W
-1

For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System.

You could build a chrome extension, see https://developer.chrome.com/docs/extensions/reference/fileSystem/

You can also open chrome with the flag --allow-file-access-from-file, see https://cmatskas.com/interacting-with-local-data-files-using-chrome/

The FileSystem API gives you a sandboxed file system created just for your web app.

Woolley answered 18/11, 2022 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.