How can I open a Windows Explorer window from JavaScript?
Asked Answered
K

3

7

I own a local PHP point of Sale, using wampp as my web-server (Win7). What I'm looking for is to find a way to open the Flash Drive E as we normally do by visiting My Computer - > USB Flash E: ... but using JavaScript.

I have found this code, which works perfectly as needed... But this only works on IE, I'm using Google Chrome as my POS browser but what Chrome does... Opens up a blank window!

Here is the code:

<script>
function CallMe()
{
 window.open("file://PCNAME/E$");
}
</script>
<html>
<input  type="button" onClick="CallMe()" value="Open USB" />
</html>

Is there an alternative way to open USB Drive E? Maybe by using PHP?

Kremenchug answered 2/1, 2013 at 9:33 Comment(6)
You're only trying to open the window, right? You aren't trying to interact with drive contents? As @madhairsilence indicates, the latter is a huge security issue. Er. Indicated. In a now-deleted comment. Just take it from me instead. Huge security risk, the latter.Inhabit
@Inhabit Yes, Just to open window.Kremenchug
-1 This is unclear. Are you trying to open the local drive's contents in the browser or some other computer on the network's contents in the browser (Javascript)? Or are you trying to open the server's folder and show it (PHP)? Or are you trying to upload a user's hard drive contents to a server? Side note: "open Local Drive C ... by using PHP" is a little scary to me.Thereof
@Inhabit okay but It works on IE ! Ok forget Drive C:, What if i want to open Flash drive? as E: ??Kremenchug
No Guys, All Maybe its my fault to add Drive C...I'll edit code above, I want to check Drive E ( USB Flash ) to check if the files transferred or not...Thats all !Kremenchug
You can create an href tag and specify file:\\\[machinetargename]\\c$, but please not this only works in IE. This will open a windows explorer window, not a browser window. Furthermore, this only works on IE, not edge, chrome or firefox for security reasons.Quadrat
I
11

You could allow the user to navigate their filesystem from the browser using:

<input type="file" />​

You can't specify a default location nor can the browser open it automatically, however.

Indispensable answered 2/1, 2013 at 9:38 Comment(4)
This opens a file picker, not a navigation tool. They are not the same thing, and act very differently.Inhabit
@Kremenchug You can't. The user must make all the moves for security reasons.Indispensable
Without being able to specify at least the default location this cannot be considered a valid solution.Lorant
Certainly a "not possible" is a valid solution, especially when it is enforced by browser vendor security implementations.Westward
R
5
window.open("file:///" + yourLocalOrNetworkPath);
Roseannroseanna answered 11/7, 2013 at 6:17 Comment(1)
This doesn't work in remote websites. It works only in local html files.Ginelle
M
2

Open file explorer in JS with :

showOpenFilePicker() function.

You can pass the arguments "desktop", "documents", "downloads", "music", "pictures", or "videos"

Handle the select fiel with :

showOpenFilePicker("documents").then((fileHandle) => {
    console.log(fileHandle);
  });

FileSystemFileHandler is returned

Full doc : here

EDIT : WILL NOT WORK ON FIREFOX

Migraine answered 1/2, 2024 at 14:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.