233rr
233rr

Reputation: 23

How to create a popup window to select a file in a local directory?

I want to create a popup window that shows me all the files in a certain directory like ex a /functions directory. I want to be able to click a file in that directory, click ok, and store it's info in variables (not upload it), How would I do this?

I've tried using a form as:

<form action="" method="post" enctype="multipart/form-data">

But with that I can't specify a directory to open specificlly, I have to manually navigate to that directory.

I've tried using window.open()

window.open("file:///" + "home/user/Desktop/demo/functions");

I've tried using an onclick link mechanism:

<a onclick="file:///+ "home/user/Desktop/demo/functions"">Open folder</a>

None of these seem to work, any ways I could approach this problem?

Upvotes: 2

Views: 2613

Answers (3)

Sonu Chauhan
Sonu Chauhan

Reputation: 7

Instead of doing this I will suggest you copy that picture in project/image folder after clicking the upload button.

Upvotes: 0

MineAndCraft12
MineAndCraft12

Reputation: 671

In JavaScript, file handling gets a bit messy. The only way to grab the contents of a folder from JavaScript would be to connect to a server and have a serverside code in a different language relay the folder information back to JavaScript.

The only way I can think that we could be able to fake this result is by placing an index.html file inside of the target directory. This index.html file would then have the names of all the files in the folder within it. However, they would have to be manually plugged into the HTML file. (if you know how to use PHP, it can scan a directory and push the contents to the HTML file)

When a web browser has to navigate to a folder, it asks the server for an index file (usually this will be an HTML or PHP file). This index would then have the contents of the folder inside of it.

If the folder is indeed on the local computer, however, there is one final way we can do this...

If the page navigates to a folder using a window.location of something akin to file:///C://Users/USERNAME/Desktop/My%20Folder/, chrome (or whatever browser you are using) will navigate to the directory and display the contents of the directory. However, since you can't put JavaScript into this browser-generated index page, you won't be able to manipulate it.

Upvotes: 1

Dave Cripps
Dave Cripps

Reputation: 929

The <input type="file"> is probably your best bet, but you can't set a default directory with it (at least not without some JavaScript voodoo, and even then there are security issues between the web and the local user).

I don't know why you would want to do that, anyway, since directory structures are going to be different between different users, and the specification of paths is different between different OS's.

Upvotes: 0

Related Questions