Skip to content

Support file saver dialog for JS and WASM targets too #348

Open

Description

@mipastgt

Hi,
it would be nice if the file saver dialog would be available for the web platforms too. An implementation could be based on the very popular FileSaver library.

Activity

  1. vinceglb commented on Aug 3, 2025

    @vinceglb
    Owner

    Hi @mipastgt! Thanks for creating this issue! I'll check it out.

  2. mipastgt commented on Aug 3, 2025

    @mipastgt
    Author

    It could be as simple as that:

    In build.gradle.kts in wasmJsMain.dependencies

    implementation(npm("file-saver", "2.0.5"))

    In your code:

    @ExperimentalWebApi
    @JsModule("file-saver")
    external class FileSaverJs {
        companion object {
            fun saveAs(blob: Blob, fileName: String)
        }
    }
    
  3. turansky commented on Aug 3, 2025

    @turansky

    It doesn't look like file-saver is highly required in current case.
    Repo looks like unsupported :(

    Following snippet can be used instead.

  4. mipastgt commented on Aug 3, 2025

    @mipastgt
    Author

    Yes, but note the comment in this more recent version of the snippet.

  5. turansky commented on Aug 3, 2025

    @turansky

    @mipastgt file-saver provides other behaviour?

  6. mipastgt commented on Aug 4, 2025

    @mipastgt
    Author

    Yes, as far as I can tell (I tried it on my mac) it always shows the dialog in the same way as you would expect it from a desktop application.

  7. vinceglb commented on Aug 20, 2025

    @vinceglb
    Owner

    I'm testing file-saver library on mac with chrome but when I call saveAs(:blob, :fileName), it doesn't open the file saver dialog. However, the file is properly downloaded in the download folder.

    It behaves the same way as FileKit.download() (https://filekit.mintlify.app/dialogs/file-saver#download-file-on-the-web)

    I think @mipastgt that in your browser settings, you have the option "Ask where to save each file before downloading" enabled as suggested in this issue: eligrey/FileSaver.js#630

    Finally, I'm not sure if forcing the browser to open to file saver dialog is possible. Did I miss something?

  8. mipastgt commented on Aug 20, 2025

    @mipastgt
    Author

    Yes, you are right. I just re-checked that and when I disable the "asking" in Chrome, then FileSaver also just saves the file in the default location.

  9. vinceglb commented on Sep 6, 2025

    @vinceglb
    Owner

    I don't think it is possible to support file saver dialogs for web targets right now. I'm closing this issue, but we can reopen if we found a way to support that in the future.

  10. turansky commented on Sep 6, 2025

    @turansky
  11. vinceglb commented on Sep 8, 2025

    @vinceglb
    Owner

    Thanks, I wasn't aware this API existed!

    Yesterday I was able to make it work in js and wasm targets on chromium based browsers. Also, I was able to create a PlatformFile from the return of showSaveFilePicker().

    But I encountered a problem when I tried to use this API on an unsupported browser like Safari: after calling showSaveFilePicker(), the Compose app crashes and didn't respond at all and log this exception in the console:

    JsException: Can't find variable: showSaveFilePicker
    

    The main problem here is that I can't catch the exception with the classic try / catch. The exception is thrown somewhere else in the internals of KMP.

    I'm not sure how to handle unsupported web APIs. Is it the responsibility of the developer to check if he can call certain web API or if it is to the library to check (and simplify the developer experience)?

  12. mipastgt commented on Sep 8, 2025

    @mipastgt
    Author
  13. MohamedRejeb commented on Jun 11, 2026

    @MohamedRejeb

    File saver is already implemented in calf, check it out that should help @vinceglb
    I'm adding a temp hidden <a> element to the DOM and trigger click programmatically.

    https://github.com/MohamedRejeb/Calf/blob/main/calf-file-picker/src/webMain/kotlin/com.mohamedrejeb.calf/picker/FileSaverLauncher.web.kt

  14. vinceglb commented on Jun 11, 2026

    @vinceglb
    Owner

    Hi @MohamedRejeb, thanks for the link!

    FileKit already does the same thing in FileKit.download() for JS/WASM.

    This works for downloading a file, but it does not force the browser to show the save dialog. The browser still decides whether to save directly to the default download folder or ask the user where to save, depending on its settings.

    The point of this issue is more about supporting a real saver dialog on web targets, like done recently with the directory picker. It should be possible, but I haven't had time to work on it recently.

    It seems possible since APIs like this exist: https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker

  15. leitingzi commented on Nov 7, 2026

    @leitingzi
    Contributor

    Hi @vinceglb!

    I'd be happy to help implement this feature!

    I've been exploring Kotlin/JS and Kotlin/Wasm Web APIs recently, and I think this would be a great addition to FileKit.

    I'd like to investigate implementing a proper file saver dialog using the showSaveFilePicker() API, with feature detection and graceful handling for browsers that don't support it.

    I can work on both JS and WASM targets, including API compatibility and tests.

    I'd be happy to work on it and submit a PR. Thanks for maintaining this great library!

  16. vinceglb commented on Nov 17, 2026

    @vinceglb
    Owner

    Hi @leitingzi, thanks for offering, and thanks again for #703!

    Yes, I'd be happy to have this. It's on the roadmap as an addition for the 1.0 release candidates: a native save dialog using the File System Access API where available, with the current download behavior as a fallback.

    Since this brings the file saver to the web public API (today openFileSaver() and rememberFileSaverLauncher() only exist on non-web targets), let's agree on the API here before you open a PR. Could you share a short proposal covering:

    1. The signature on web. Ideally the same FileKit.openFileSaver(suggestedName, defaultExtension, directory, dialogSettings) as on other platforms, so it can move to common code. Which parameters can't be supported?
    2. What the returned PlatformFile is, and how write() works on it (today it only works on OPFS files).
    3. The fallback on browsers without showSaveFilePicker() (Firefox, Safari): what does the call return, and how does the app still save its bytes?
    4. Cancellation (AbortError) and the user activation requirement.

    Once we agree, a first PR with filekit-dialogs and tests in webTest would be perfect. The Compose launcher can follow.

  17. leitingzi commented on Nov 17, 2026

    @leitingzi
    Contributor

    Hi @vinceglb, thanks for the detailed feedback! Here's my proposed approach:

    1. API:
      Keep the same FileKit.openFileSaver(...) signature across platforms. Support suggestedName and defaultExtension, while documenting unsupported Web-specific parameters such as directory and dialogSettings.

    2. PlatformFile:
      For browsers supporting the File System Access API, I'd introduce a Web-specific PlatformFile implementation backed by FileSystemFileHandle.
      Its write() implementation would use FileSystemFileHandle.createWritable() to write directly to the selected file, rather than trying to reuse the existing OPFS-based implementation.
      This would also keep the returned PlatformFile consistent with the existing FileKit API, allowing callers to use the usual file-writing operations.

    3. Fallback:
      Use showSaveFilePicker() when available, and fall back to the existing download mechanism otherwise.

    4. Cancellation:
      Treat AbortError as normal cancellation, propagate other errors

    I'll implement JS and wasmJs separately using their respective interop APIs, with tests in webTest. The Compose launcher can follow in a separate PR.

    Let me know if this design looks good to you!

  18. leitingzi commented on Nov 17, 2026

    @leitingzi
    Contributor
    internal sealed interface WebFile {
    
        // Existing implementations...
    
        class FileSystemAccessFile(
            val handle: FileSystemFileHandle,
        ) : WebFile
    }

    I would like to add a new type here.

  19. vinceglb commented on Nov 17, 2026

    @vinceglb
    Owner

    Thanks @leitingzi, the direction looks good, and a new WebFile subtype is the right idea. Here is what I'd like, with a few changes. I've updated the roadmap to match (#720).

    1. Same API as on other platforms

    Keep the exact openFileSaver signature, declared once in webMain (shared by JS and Wasm) and marked @ExperimentalFileKitApi for now. allowedExtensions maps to the dialog's types option (reuse buildFileSaverAllowedFileTypes). directory and dialogSettings are ignored on web, and documented as such. Later, I'll move openFileSaver, write and writeString to common code.

    The goal is that this code works in every browser:

    val file = FileKit.openFileSaver(suggestedName = "report", defaultExtension = "pdf")
    file?.write(bytes)

    2. The returned file

    Something like this in filekit-core:

    public sealed class WebFile {
        // Existing subtypes...
    
        @ExperimentalFileKitApi
        public class SaveDestination internal constructor(
            internal val handle: FileSystemFileHandle?, // null in download mode (see 3)
            public val name: String,
        ) : WebFile() {
            public val isDownload: Boolean get() = handle == null
            // type, size, lastModified: snapshot refreshed after each write, like OriginPrivateFile
        }
    }
    
    // Used by filekit-dialogs, hidden from apps
    @FileKitCoreInternalApi
    public fun createSaveDestination(handle: JsAny?, name: String): PlatformFile

    Compared to your snippet:

    • WebFile is a public sealed class, so the new subtype is public API, hence @ExperimentalFileKitApi.
    • FileSystemFileHandle is internal, so handle and the constructor stay internal. filekit-dialogs goes through the factory, marked with a new @FileKitCoreInternalApi opt-in (same idea as @FileKitDialogsInternalApi).
    • SaveDestination rather than FileSystemAccessFile, because it also covers the download mode, where there is no handle.

    Please share the handle write code with OriginPrivateFile instead of duplicating it.

    3. Fallback: download on write

    When the dialog would open, the app has no bytes yet, so we can't download at that point. On browsers without showSaveFilePicker() (Firefox, Safari) and in cross-origin iframes, openFileSaver() returns right away a SaveDestination in download mode. Its write() downloads the bytes with the suggested name. Reading it never throws: it returns the last written content.

    4. Errors

    • AbortError (the user cancelled): return null.
    • Anything else, like SecurityError when there was no user click: throw FileKitDialogException.

    Please do the feature check and the call in a single js(...) function that never throws and returns { handle } or { error }. When I tried this API last year, Safari threw Can't find variable: showSaveFilePicker, and a Kotlin try/catch couldn't catch it (comment above).

    5. Tests and scope

    The real picker can't run in headless Chrome, so put it behind a small internal seam and test with a fake in webTest: picker unavailable, cancelled, error, and a picked handle (an OPFS handle works). Please list your manual checks (Chrome, Firefox, Safari) in the PR description.

    • PR 1: filekit-core + filekit-dialogs + tests + docs.
    • PR 2: rememberFileSaverLauncher on web.

    Does this work for you?

  20. leitingzi commented on Nov 18, 2026

    @leitingzi
    Contributor

    Thanks, this works for me. I checked the current web core, and FileKit.download() only triggers a browser download; PlatformFile.write() currently only works with OPFS files. So a SaveDestination that supports both a file handle and download mode makes sense.

    I’ll follow the API and error handling you outlined, add the fake-picker webTest cases, and keep the Compose launcher out of this PR. I’ll also include the Chrome, Firefox, and Safari manual checks in the PR description.

  21. leitingzi commented on Nov 18, 2026

    @leitingzi
    Contributor

    Hi @vinceglb I feel like I’ve finished implementing the file-saving dialog functionality for the web platform.

    achieved the goal:

    val file = FileKit.openFileSaver(suggestedName = "report", defaultExtension = "pdf")
    file?.write(bytes)

    I tested the Edge, Chrome, and Firefox browsers myself. Edge and Chrome can save files as expected, whereas Firefox reverts to downloading them. The results are in line with expectations. However, I don’t have access to a MacBook at the moment, so I can’t check how it looks in Safari.

    I have also updated the sample app. You can use the following URL to test file saving on the web-based version. You can use the file-saving feature in your browser to save a TXT file containing content FileKit web file saver sample.
    https://leitingzi.github.io/FileKit/

    An issue has been identified: it is possible to write to a .txt file, but not to a .pdf file.

    I have added my code only to the webMain source sets for core and dialog. You can view the scope of these changes via the following link: leitingzi@95e6180#diff-6a4455d3642f70f9abf3f10035f83e288eec33088a3fd1e77ae2fb1fd4c75b98

    Some parts of the implementation still need refinement, including error handling, tests, and documentation. I'll work on these improvements and submit a PR in the next few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfeature requestFeature asked by the community

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions