wails/v3/examples/file-input/main.go
Lea Anthony 30ffbba060
feat(v3): add file-input-test example for #4862 (#4950)
* feat(v3): add file-input example for issue #4862

Minimal example demonstrating HTML file input functionality:
- Single file selection
- Multiple file selection
- Files or directories (webkitdirectory)
- Accept filter (note: not enforced by macOS)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor(example): use JS runtime dialog API instead of Go backend

Update file-input example to use wails.Dialogs.OpenFile() from the
JS runtime instead of a custom Go FileService backend. This demonstrates
the recommended approach for dialog functionality.

Fixes #4862

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor(example): use generated bindings for dialog API

Update file-input example to use proper generated bindings instead
of inline JS runtime calls. The example now demonstrates:
- HTML file input elements (single, multiple, webkitdirectory)
- Wails Dialog API via generated FileService bindings

Fixes #4862

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: add changelog entry for macOS file input fix

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 07:29:15 +11:00

69 lines
1.4 KiB
Go

package main
import (
"embed"
"log"
"github.com/wailsapp/wails/v3/pkg/application"
)
//go:embed assets/*
var assets embed.FS
type FileService struct{}
func (f *FileService) OpenDirectoryOnly() string {
result, err := application.Get().Dialog.OpenFile().
CanChooseDirectories(true).
CanChooseFiles(false).
SetTitle("Select Directory").
PromptForSingleSelection()
if err != nil {
return "Error: " + err.Error()
}
if result == "" {
return "Cancelled"
}
return result
}
func (f *FileService) OpenFilteredFile() string {
result, err := application.Get().Dialog.OpenFile().
SetTitle("Select Text File").
AddFilter("Text Files", "*.txt").
PromptForSingleSelection()
if err != nil {
return "Error: " + err.Error()
}
if result == "" {
return "Cancelled"
}
return result
}
func main() {
app := application.New(application.Options{
Name: "File Input Test",
Description: "Test for HTML file input (#4862)",
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
Services: []application.Service{
application.NewService(&FileService{}),
},
Assets: application.AssetOptions{
Handler: application.BundledAssetFileServer(assets),
},
})
app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "File Input Test",
Width: 700,
Height: 500,
URL: "/",
})
if err := app.Run(); err != nil {
log.Fatal(err)
}
}