2

I have a Windows application based on Qt WebEngineView (in QML). There are web pages in my app. Some web page need user to select files using:

<input type="file" />

I want to know, is it possible for my app to remember the last directory user selected a file?

I have tried:

  • Set value property to <input> in my web page, but it is not allowed by browser to set it programmatically, see Set default value for a input file form.
  • Open my web pages in Chrome, Chrome can remember last directory. But in my app, every time it opens the directory where the app is installed.
  • In native Open File Dialog, I can set an initial directory, but can I do the same for Open File Dialog in web pages?

I know this may be impossible. Any suggestions and workarounds are appreciated.

Community
  • 1
  • 1
zhm
  • 3,513
  • 3
  • 34
  • 55

1 Answers1

3

The WebEngineView element has a signal for when the Web content requests a file dialog.

The FileDialog element has a property for setting (and getting) the folder.

So something along these lines should work

FileDialog {
    id: dialog

    property var request

    onRejected: request.dialogReject()

    onAccepted: {
        yourSavedFolder = folder;

        request.dialogAccept(files);
    }
}

WebEngineView {
    onFileDialogRequested: {
        request.accepted = true; // inhibit default dialog

        dialog.request = request;
        dialog.folder = yourSavedFolder;
        dialog.open()
    }
}

This is just a rough sketch, you'll also need to handle the open mode of the request object, etc.

Kevin Krammer
  • 5,159
  • 2
  • 9
  • 22
  • 1
    Very helpful! **Note:** **1.** `onFileDialogRequested` needs Qt 5.8. **2.** `request.dialogAccept(files);` should be `request.dialogAccept(fileUrl.toString());` (in single selection mode) – zhm Mar 04 '17 at 18:21