You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support file saver dialog for JS and WASM targets too #348
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.
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.
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?
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.
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:
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)?
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.
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.
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!
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:
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?
What the returned PlatformFile is, and how write() works on it (today it only works on OPFS files).
The fallback on browsers without showSaveFilePicker() (Firefox, Safari): what does the call return, and how does the app still save its bytes?
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.
Hi @vinceglb, thanks for the detailed feedback! Here's my proposed approach:
API:
Keep the same FileKit.openFileSaver(...) signature across platforms. Support suggestedName and defaultExtension, while documenting unsupported Web-specific parameters such as directory and dialogSettings.
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.
Fallback:
Use showSaveFilePicker() when available, and fall back to the existing download mechanism otherwise.
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.
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:
publicsealedclassWebFile {
// Existing subtypes...
@ExperimentalFileKitApi
publicclassSaveDestination internal constructor(
internalvalhandle:FileSystemFileHandle?, // null in download mode (see 3)publicvalname:String,
) : WebFile() {
publicval isDownload:Boolean get() = handle ==null// type, size, lastModified: snapshot refreshed after each write, like OriginPrivateFile
}
}
// Used by filekit-dialogs, hidden from apps
@FileKitCoreInternalApi
publicfuncreateSaveDestination(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.
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.
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.
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.
Activity
vinceglb commentedon Aug 3, 2025
Hi @mipastgt! Thanks for creating this issue! I'll check it out.
mipastgt commentedon Aug 3, 2025
It could be as simple as that:
In build.gradle.kts in wasmJsMain.dependencies
implementation(npm("file-saver", "2.0.5"))In your code:
turansky commentedon Aug 3, 2025
It doesn't look like
file-saveris highly required in current case.Repo looks like unsupported :(
Following snippet can be used instead.
mipastgt commentedon Aug 3, 2025
Yes, but note the comment in this more recent version of the snippet.
turansky commentedon Aug 3, 2025
@mipastgt
file-saverprovides other behaviour?mipastgt commentedon Aug 4, 2025
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.
vinceglb commentedon Aug 20, 2025
I'm testing
file-saverlibrary on mac with chrome but when I callsaveAs(: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?
mipastgt commentedon Aug 20, 2025
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.
vinceglb commentedon Sep 6, 2025
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.
turansky commentedon Sep 6, 2025
Works in Chrome - https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker
vinceglb commentedon Sep 8, 2025
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
PlatformFilefrom the return ofshowSaveFilePicker().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: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)?
mipastgt commentedon Sep 8, 2025
Have you tried following this recommendation?
https://developer.chrome.com/docs/capabilities/web-apis/file-system-access#feature_detection
MohamedRejeb commentedon Jun 11, 2026
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
vinceglb commentedon Jun 11, 2026
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
leitingzi commentedon Nov 7, 2026
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!
vinceglb commentedon Nov 17, 2026
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()andrememberFileSaverLauncher()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:FileKit.openFileSaver(suggestedName, defaultExtension, directory, dialogSettings)as on other platforms, so it can move to common code. Which parameters can't be supported?PlatformFileis, and howwrite()works on it (today it only works on OPFS files).showSaveFilePicker()(Firefox, Safari): what does the call return, and how does the app still save its bytes?AbortError) and the user activation requirement.Once we agree, a first PR with
filekit-dialogsand tests inwebTestwould be perfect. The Compose launcher can follow.leitingzi commentedon Nov 17, 2026
Hi @vinceglb, thanks for the detailed feedback! Here's my proposed approach:
API:
Keep the same FileKit.openFileSaver(...) signature across platforms. Support suggestedName and defaultExtension, while documenting unsupported Web-specific parameters such as directory and dialogSettings.
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.
Fallback:
Use showSaveFilePicker() when available, and fall back to the existing download mechanism otherwise.
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!
leitingzi commentedon Nov 17, 2026
I would like to add a new type here.
vinceglb commentedon Nov 17, 2026
Thanks @leitingzi, the direction looks good, and a new
WebFilesubtype 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
openFileSaversignature, declared once inwebMain(shared by JS and Wasm) and marked@ExperimentalFileKitApifor now.allowedExtensionsmaps to the dialog'stypesoption (reusebuildFileSaverAllowedFileTypes).directoryanddialogSettingsare ignored on web, and documented as such. Later, I'll moveopenFileSaver,writeandwriteStringto common code.The goal is that this code works in every browser:
2. The returned file
Something like this in
filekit-core:Compared to your snippet:
WebFileis a public sealed class, so the new subtype is public API, hence@ExperimentalFileKitApi.FileSystemFileHandleis internal, sohandleand the constructor stay internal.filekit-dialogsgoes through the factory, marked with a new@FileKitCoreInternalApiopt-in (same idea as@FileKitDialogsInternalApi).SaveDestinationrather thanFileSystemAccessFile, because it also covers the download mode, where there is no handle.Please share the handle write code with
OriginPrivateFileinstead 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 aSaveDestinationin download mode. Itswrite()downloads the bytes with the suggested name. Reading it never throws: it returns the last written content.4. Errors
AbortError(the user cancelled): returnnull.SecurityErrorwhen there was no user click: throwFileKitDialogException.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 threwCan't find variable: showSaveFilePicker, and a Kotlintry/catchcouldn'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.filekit-core+filekit-dialogs+ tests + docs.rememberFileSaverLauncheron web.Does this work for you?
leitingzi commentedon Nov 18, 2026
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.
leitingzi commentedon Nov 18, 2026
Hi @vinceglb I feel like I’ve finished implementing the file-saving dialog functionality for the web platform.
achieved the goal:
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
webMainsource sets forcoreanddialog. You can view the scope of these changes via the following link: leitingzi@95e6180#diff-6a4455d3642f70f9abf3f10035f83e288eec33088a3fd1e77ae2fb1fd4c75b98Some 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.
📝 Update roadmap for the web file saver