htmlqmlqtwebengineqt5.7

How to make web page remember last open file directory?


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:

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


Solution

  • 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.