Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-126101

wasm: Local file access using standard QFileDialog API

    XMLWordPrintable

Details

    • Task
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 6.9
    • Other
    • None
    • WebAssembly

    Description

      Intro

      The expected way to access local files in Qt Code is to use the QFileDialog API (widgets or QML) to show a file dialog to the user, and then access the file using the returned file path or url.

      QString filePath = QFileDialog::getOpenFileName("file.txt");

      ...

      QFile file(filePath); // open and read file

      However, this is not supported on by Qt WebAssembly, to access local files on the host file system, for two main reasons:

      • The Web file dialog API returns a File object instead of a path. The File object is the only way to access file content; opening a file from a path is not supported.
      • The Web File API is asynchronous when reading or writing to a file.

      Background

      The Web platform provides access to local files through the File System API. (https://developer.mozilla.org/en-US/docs/Web/API/File_System_API) This API provides functionality for opening a native file dialog, where the user can give the web application access to files.

      File access is provided through the Web File API (https://developer.mozilla.org/en-US/docs/Web/API/File). This is a non-POSIX file API, which does not map directly to the current QFile abstraction:

      • Web File is capability based: opening a file via a path is not possible; the File object obtained from the file dialog must be used
      • Web File is async: read/write calls complete via JS Promise

      Current Support for QFileDialog on WebAssembly

      QFileDialog provides two functions for reading and writing file content: These are supported on all platforms with a special implementation for WebAssembly and a generic fallback for other platforms.{}

      QByteArray QFileDialog::getOpenFileContent();
      QFileDialog::saveFileContent(QByteArray);

      Using this API makes local file access possible today, but has a couple of limitations:

      • Can be difficult to use from existing code bases which works with QFile and file paths, especially from QML.
      • Has no support for partial file reads or writes (useful when working with large files)

       

      Related Emscripten / Web Platform work

      Qt Development Tasks

      • Add a native backend for QFileDialog
        • Support the async QFileDialog API (the fileSelected signal)
        • Support QML usage (also async)
        • Support the synchronous QFileDialog API (the static functions)
      • Find a way to handle File objects
        • TODO

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              evenandersen Even Oscar Andersen
              sorvig Morten Sørvig
              Votes:
              2 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes