How to open a file chooser on the server?

In my application, I need the user to be able to copy a file from one location to another on the server. Is there any way to open a file chooser that works like the Swing JFileChooser?

All I need to get is the path to the chosen file.

Upload works on the client side, so that makes life difficult. It would require that the server directories be mapped to the client somehow. Then I would have to re-map the file to match its actual location on the server. Not an ideal situation. :wink:

I think I have solved it. The look and feel are not “Cuba”, but this code does work:

JFileChooser jfc = new JFileChooser();
jfc.setCurrentDirectory(new File(String.format("/pas/merges/%07d/data/%s", mplist.getOrdnum(), mplist.getKeycode())));
jfc.setDialogTitle("Choose EMailed file to Copy");
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.setAcceptAllFileFilterUsed(true);
if (jfc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
       notifications.create(Notifications.NotificationType.TRAY)
             .withCaption("Copy Aborted")
              .show();
        return
}
String filename = jfc.getSelectedFile().getAbsolutePath();

image

Anybody have a better suggestion?

Hi, you cannot open it in web browser if your code runs on another server. Just because it is not possible at all with Swing. Your current result works only on your machine.

I see. Any suggestions for a possible solution?

Hi,

as @jreznot said: it is basically impossible through the browser and native “File Open” Operating system functionalities. Otherwise it would be a big security flaw.

Therefore the only way to do it to create the abilities programmatically.

A while ago I created an example that shows a directory listing from the server through a via the Tree component. I have updated it to CUBA 6.10 and added some features.

directory-listing-overview

You can find it on Github: GitHub - mariodavid/cuba-example-directory-listing: CUBA example that shows how to display a directory on the file system of the server and create files on it

I hope it helps you to understand how to interact with the filesystem of the server. Copying files from one place to another would be to create another similar thing as the “create file” capabilities I showed in the example.

Cheers
Mario

2 Likes

Thank you. I will check it out.