2

We have a Java (JSP) web application and I want (well, the client) wants when they upload a file , based on a specific condition , the file picker to open directly in the D drive root directory. Also when they download a file, the file to be downloaded automatically by default in the same D (USB) drive (not universally through browser settings of course).

I know that this is not possible due to security reasons but I recently saw about this File System Access API and I am trying to understand what is possible.

Well, apparently we can use

async function fileOpen() {
            const fileHandle = await window.showOpenFilePicker({
                startIn: 'documents'
            });
        }

but in the startIn property whenever I try setting D:\ or anything other than the WellKnownDirectories I get the error:

TypeError: Failed to execute 'showOpenFilePicker' on 'Window': Failed to read the 'startIn' property from 'FilePickerOptions': The provided value 'D:\' is not a valid enum value of type WellKnownDirectory.

So is something like that possible? The file picker that opens from the <input type=file> doesn't support setting any value for the default directory so this is why I am looking at the file system access api.

Keep in mind that this is a controlled environment and the existence of the D: drive is ensured

Olivier
  • 13,283
  • 1
  • 8
  • 24
Marios
  • 339
  • 1
  • 14
  • Have you tried a browser extension? I don't if it's possible, but I would check it. The extension API provides many more possibilities. Do you have documentation for the `startIn` property? I can't find it. – jabaa Jun 23 '23 at 11:00
  • You can find info here : https://developer.chrome.com/articles/file-system-access/#ask-the-user-to-pick-a-file-to-read No I don't want to try browser extensions as this requires installation in so many client's PCs. It has to be done programmatically – Marios Jun 23 '23 at 11:26
  • 2
    “this is a controlled environment”—not by you, as it seems. If the user wants the downloads’ default directory to be `D:`, the user has to configure their browser accordingly. It’s part of the controlled environment. And a file picker is not an automatic download anyway… Besides that, this doesn’t seem to be a Java question at all, so the `java` tag is not appropriate. – Holger Aug 24 '23 at 17:23
  • @Holger it's not that the user wants the download directory to be universally the D drive. Based on a specific condition , they want the file picker to automatically open in the D drive when they upload a file, the same when they download. I know that in the browser setting you can specify the default Downloads directory but that is not exactly what the want. The Java tag was just because this is a Java web app . So to your knowledge is this something that can't be achieved by JS alone using the File System Access API? – Marios Aug 25 '23 at 06:35
  • 2
    Well, you already wrote “I know that this is not possible due to security reasons”, so our knowledge is the same. I do not expect to find something in the specification saying “this (oddly specific) example contradicting our understanding of security (also) doesn’t work”. – Holger Aug 25 '23 at 10:10
  • This had to do mainly with the functionality of `input type='file'` which doesn't allow you to do anything at all with the file picker. The new API gives you more customization options and this is where my question lies. If , someway, lets you decide a default upload directory/drive – Marios Aug 25 '23 at 10:13
  • 2
    Regardless of how you phrase your question, the fundamental problem persists: nobody can show you a proof that something doesn’t exist. You already found out that the `startIn` property only allows enum constants of the speaking type `WellKnownDirectory`. The new API might have more flexibility, but the fundamental principle doesn’t change, you are not supposed to know whether a path `D:` exists. – Holger Aug 25 '23 at 13:01

2 Answers2

3

The best thing you can do with showOpenFilePicker/showSaveFilePicker (i.e. without requesting acess to directories) is keeping the same directory between pickers:

  1. You can't request initial path in showOpenFilePicker, so the first time you open file picker user have to navigate do D:/ themselves.
    (you may provide WellKnownDirectory("documents"/...) startIn) to chose where user starts from
  2. The second time you open file picker, you may request browser open file picker in the same location by provoding it the same id
await showOpenFilePicker({ id:"thisGonnaBeDDrive" })
// this opens default directory (socuments or whatever)
// user opens `D:/foo/myfile.bar`
await showOpenFilePicker({ id:"thisGonnaBeDDrive" })
// this opens *the same* directory used last time - `D:/foo/`
await showSaveFilePicker({ id:"thisGonnaBeDDrive" })
// this opens *the same* directory used last time - `D:/foo/`
  1. Alternatively, you can use the FileSystemHandle you got to open file picker in the same location
let [handle] = await showOpenFilePicker()
// user opens `D:/foo/myfile.bar`
showOpenFilePicker({ startIn: handle })
// this opens directory of *the handle* - `D:/foo/`

startIn handle has a priority over id
id has a priority over WellKnownDirectory("documents"/...) startIn

Dimava
  • 7,654
  • 1
  • 9
  • 24
1

The startIn parameter of the showOpenFilePicker function in JavaScript can only be set to a WellKnownDirectory.

This limitation is in place for security reasons, as it ensures a secure and consistent user experience. By restricting the parameter to well-known directories, potential security risks are prevented, and users can easily navigate to commonly used directories. Allowing unrestricted access to any directory could potentially lead to malicious activities.

Reza Kia
  • 400
  • 1
  • 7