Open and save selected documents
Unreleased preview.3 candidate. These APIs are absent from the published preview.2 SDK. Keep its released declarations and packages unchanged. There is no new public SDK download on this page.
Use the optional @LuiWorkbench/Files module for documents the user selects.
Use File:ByteLength(Callback) for the current byte length and File:ReadRange(Position, Count, Callback) to process a large document incrementally. Position is zero-based. Count is 1–65,536. The callback receives a buffer and an optional failure code. Offsets at or beyond EOF produce an empty buffer. A file may change between reads. The headless file-summary SDK example accepts an operator-selected file through --read-file.
For uncommitted writes to a selected save destination, use File:BeginWriter(Callback). Its seekable writer accepts 64 KiB chunks, up to 128 MiB per draft. Commit replaces the destination; Close discards an unfinished draft. See the packaged atomic-copy example.
A headless host can fulfill OpenFolder with an explicitly writable directory using --write-directory DIRECTORY. Use Folder:OpenOutput, CreateDirectory, RemoveFile, RemoveDirectory (empty only) and Rename(From, To, Callback) for direct children. Rename refuses an existing destination. The --read-directory grant and desktop folder pickers expose reads and listing. Files.Copy(Source, Destination, Callback) streams 64 KiB at a time and publishes the replacement on commit. Closing either file ends the copy. Disposing either picker cancels it. See the candidate’s folder-copy project.
For sequential work, create a cursor with Files.Reader(File, Offset?). Each successful Reader:Read(Count, Callback) advances its position by the delivered length. Reader:Position() and Reader:Seek(Offset) use zero-based byte offsets. Canceling a pending read preserves its starting position. Close each cursor, then close the file when all readers finish.
Use Preferences for app-private settings or a scratchpad. The two grants serve different purposes; a native path string from preview.2’s legacy picker does not grant filesystem access.
The complete candidate Examples/selected-files project includes its vendor
files, module lock and Dialogs capability. Copy the whole folder outside the
SDK, then run lui check and lui run --mode project with that candidate SDK.
The native WinUI3/GTK4.10+ host needs a visible parent window for a picker.
Ordinary apps need no C++ code, provider registration or native file paths.
--!strictlocal Files = require('@LuiWorkbench/Files')local Picker = Files.New(app:GetService('FileService'))local Window = Instance.new('Window', {Title='Selected files', Size=UDim2.fromOffset(620,440)})local Editor = Instance.new('TextBox', {Parent=Window, Multiline=true, MaxLength=16000, Position=UDim2.fromOffset(16,64), Size=UDim2.new(1,-32,1,-128)})local Status = Instance.new('TextLabel', {Parent=Window, Text='Open a UTF-8 text file or write a note.', TextWrapped=true, Position=UDim2.new(0,16,1,-60), Size=UDim2.new(1,-32,0,48)})local Busy, Alive = false, truelocal function Button(Text: string, Index: number, Action: () -> ()) local Value = Instance.new('TextButton', {Parent=Window, Text=Text, Position=UDim2.new((Index-1)/3,16,0,16), Size=UDim2.new(1/3,-22,0,36)}) Value.Activated:Connect(function() if not Busy then Action() end end)endButton('Open file',1,function() Busy = true local Before = Editor.Text Picker:OpenFile(function(File, Failure) if not File then Busy=false; Status.Text=Failure or 'Selection cancelled.'; return end File:Read(function(Bytes, ReadFailure) File:Close(); Busy=false if not Bytes then Status.Text=ReadFailure or 'Read failed.'; return end local Text = buffer.tostring(Bytes) if #Text>16000 or not utf8.len(Text) or string.find(Text,'\0',1,true) then Status.Text='Choose a UTF-8 text file of at most16,000 bytes.'; return end if Editor.Text~=Before then Status.Text='Newer edits were kept. Open again when ready.'; return end Editor.Text=Text; Status.Text='Opened '..File.Name..'. Save as writes a copy.' end) end)end)Button('Save as',2,function() if #Editor.Text>16000 then Status.Text='Shorten the text to16,000 UTF-8 bytes before saving.'; return end Busy = true local Text = Editor.Text -- Capture now; later edits do not change this save. Picker:SaveFile('note.txt',function(File, Failure) if not File then Busy=false; Status.Text=Failure or 'Save cancelled.'; return end File:Write(buffer.fromstring(Text),function(WriteFailure) File:Close(); Busy=false Status.Text=if WriteFailure then 'Save could not be confirmed; your text is kept.' elseif Editor.Text~=Text then 'Saved the earlier version. New edits still need saving.' else 'File saved.' end) end)end)Button('Open folder',3,function() Busy = true Picker:OpenFolder(function(Folder, Failure) if not Folder then Busy=false; Status.Text=Failure or 'Selection cancelled.'; return end Folder:List(function(Entries, ListFailure) Folder:Close(); Busy=false Status.Text=if Entries then tostring(#Entries)..' entries in '..Folder.Name else ListFailure or 'List failed.' end) end)end)Window.CloseRequested:Connect(function() if Busy then Window:PreventClose(); Status.Text='Wait for the current file task.' end end)local function Retire() if not Alive then return end; Alive=false; Picker:Dispose() endWindow.Destroying:Connect(Retire)Window.Closed:Connect(function() Retire(); Window:Destroy() end)Window.Visible=trueOpen file returns a read-only File. Save as asks for a destination and returns a writable File; Write accepts a buffer and reports completion through its callback. This example captures text before opening the picker and keeps newer edits when the operation finishes. File Desk adds app-private storage and dirty-close guards. Always close a delivered File/Folder, and dispose the Picker when its owner is destroyed. Native handles can keep files or folders from being renamed on Windows. Close is safe to repeat. A closed wrapper can stay in your recent-files list; the module drops its owned handle reference. Native token reclamation still depends on ordinary garbage collection and the host’s retirement pump.
Open folder returns a read-only Folder. Folder:List(Complete) returns records
with Name, IsDirectory and Size. Folder:Open(Name, Complete) opens one regular
child file for reading. Pass one leaf name, such as note.txt; traversal, nested
paths, folder writes and recursive walking are not part of this profile. Folder
grants are not saved across application restarts.
Every async method returns an Operation. Operation:Cancel() returns true once
while active and suppresses that module callback; Picker Dispose also cancels
operations and closes owned resources. A user’s picker dismissal calls Complete
with nil/nil. Other failures arrive as a fixed code: Unavailable for admission
refusal, or native CapabilityDenied, InvalidArgument, Unsupported, WouldBlock, LimitExceeded, Canceled,
DeadlineExceeded, Closed or ProviderFailure. Catch expected programming errors
with pcall; calling Read after Close or Write on a read-only File throws a fixed
[LUI:Files] error. Callback failures are contained and newly delivered resources
are closed; raw application errors are not printed by the module.
Cancellation settles the request before native dialog teardown necessarily ends.
Starting another picker during teardown can report WouldBlock; keep the editor
text and let the user retry rather than treating it as a lost document.
File buffers are limited to4MiB, with32 provider resources/requests plus existing native domain quotas. A folder list has at most256 entries, scanning at most1024; oversize directories fail instead of silently returning a partial list. Leaf names use at most255 UTF-8 bytes and portable filename rules. IO has a30s deadline and pickers120s. These are service ceilings; editors should choose smaller UI limits.
The qualified target is local regular files. Links/reparse paths, hardlinked files, whole-drive roots, direct UNC paths and devices are refused. Mounted/mapped network filesystems are not detected portably and remain unqualified. Link-backed/cloud-only documents, browser/mobile hosts and other file system profiles are unsupported. Windows/Linux owner and filesystem tests are distinct from real picker, keyboard, clipboard and physical desktop qualification; consult the candidate’s evidence.
Save as atomically replaces one selected data entry using a new file. It does not preserve the old file’s ACLs, ownership, extended attributes or other metadata. An observed external change refuses the save; this is not compare-and-swap against another process racing the final rename. Cancellation cannot undo a committed write, and an error after replacement can leave the new file in place. Keep the editor text and report an uncertain save without claiming that nothing changed.
The candidate project checker also catches unknown or readonly keys in direct
literal Instance.new('Class', {Properties}) calls. Dynamic tables/classes, aliases,
value compatibility and legacy single-file checking still require runtime
validation. The Properties:any declaration is not full initializer type checking.
Files module types for application authors
export type Operation = {read Cancel: (Self: Operation) -> boolean}export type Entry = {read Name: string, read IsDirectory: boolean, read Size: number}export type Writer = { read Position: (Self: Writer) -> number, read Seek: (Self: Writer, Offset: number) -> (), read Write: (Self: Writer, Bytes: buffer, Complete: (string?) -> ()) -> Operation, read Commit: (Self: Writer, Complete: (string?) -> ()) -> Operation, read Close: (Self: Writer) -> (),}export type File = { read Name: string, read Writable: boolean, read Read: (Self: File, Complete: (buffer?, string?) -> ()) -> Operation, read ReadRange: (Self: File, Position: number, Count: number, Complete: (buffer?, string?) -> ()) -> Operation, read ByteLength: (Self: File, Complete: (number?, string?) -> ()) -> Operation, read BeginWriter: (Self: File, Complete: (Writer?, string?) -> ()) -> Operation, read Write: (Self: File, Bytes: buffer, Complete: (string?) -> ()) -> Operation, read Close: (Self: File) -> (),}export type Folder = { read Name: string, read List: (Self: Folder, Complete: ({Entry}?, string?) -> ()) -> Operation, read Open: (Self: Folder, Name: string, Complete: (File?, string?) -> ()) -> Operation, read OpenOutput: (Self: Folder, Name: string, Complete: (File?, string?) -> ()) -> Operation, read CreateDirectory: (Self: Folder, Name: string, Complete: (string?) -> ()) -> Operation, read RemoveDirectory: (Self: Folder, Name: string, Complete: (string?) -> ()) -> Operation, read RemoveFile: (Self: Folder, Name: string, Complete: (string?) -> ()) -> Operation, read Rename: (Self: Folder, Name: string, Destination: string, Complete: (string?) -> ()) -> Operation, read Close: (Self: Folder) -> (),}export type Reader = { read Read: (Self: Reader, Count: number, Complete: (buffer?, string?) -> ()) -> Operation, read Seek: (Self: Reader, Offset: number) -> (), read Position: (Self: Reader) -> number, read Close: (Self: Reader) -> (),}export type Picker = { read OpenFile: (Self: Picker, Complete: (File?, string?) -> ()) -> Operation, read SaveFile: (Self: Picker, SuggestedName: string, Complete: (File?, string?) -> ()) -> Operation, read OpenFolder: (Self: Picker, Complete: (Folder?, string?) -> ()) -> Operation, read Dispose: (Self: Picker) -> (),}Advanced native provider surface
FileService uses the established owned-buffer, NativeHandle and NativeRequest protocol. These methods are for framework/provider integrations; ordinary app code should use the Files module above. A trusted desktop host installs the additive LuiFiles.h picker bridge before application entry. Dialogs is checked again after project authority is narrowed. Providers never accept an app-supplied native path.
export type FileService = { read OpenFile: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read SaveFile: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read OpenFolder: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read Read: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read Write: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read List: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read Open: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read ReadRange: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read ByteLength: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read BeginWriter: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read WriteAt: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read CommitWriter: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read OpenOutput: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read CreateDirectory: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read RemoveDirectory: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read RemoveFile: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest, read Rename: (Self: FileService, Input: buffer, Handle: NativeHandle?, Complete: (buffer?, NativeHandle?, string?) -> (), Progress: ((buffer) -> ())?) -> NativeRequest,}