More Info: https://web.dev/file-system-access/The File System Access API allows web apps to read or save changes directly to files and folders on the user's device.
Code: Select all
EnableExplicit
Global FileHandle
Enumeration
#Window
#LoadButton
#SaveButton
#SaveAsButton
#Editor
EndEnumeration
Procedure IsUndefined(Object)
! return typeof v_object === "undefined";
EndProcedure
Procedure IsDefined(Object)
! return typeof v_object !== "undefined";
EndProcedure
Procedure WriteFile(FileHandle, Content.s)
! (async() => {
! // Create a FileSystemWritableFileStream to write to.
! const writable = await v_filehandle.createWritable();
! // Write the contents of the file to the stream.
! await writable.write(v_content);
! // Close the file and write the contents to disk.
! await writable.close();
! })(v_filehandle, v_content);
EndProcedure
Procedure LoadButtonEvent()
Protected FileContent.s
! (async() => {
! [v_filehandle] = await window.showOpenFilePicker();
! const file = await v_filehandle.getFile();
! v_filecontent = await file.text();
SetGadgetText(#Editor, FileContent)
SetActiveGadget(#Editor)
! })();
EndProcedure
Procedure SaveAsButtonEvent()
Protected FileContent.s = GetGadgetText(#Editor)
! (async() => {
! const options = {
! types: [
! {
! description: 'Text Files',
! accept: {
! 'text/plain': ['.txt'],
! },
! },
! ],
! };
! v_filehandle = await window.showSaveFilePicker(options);
If IsDefined(FileHandle)
WriteFile(FileHandle, FileContent)
EndIf
! })();
EndProcedure
Procedure SaveButtonEvent()
If IsUndefined(FileHandle) Or FileHandle = 0
SaveAsButtonEvent()
ProcedureReturn
EndIf
Protected FileContent.s = GetGadgetText(#Editor)
WriteFile(FileHandle, FileContent)
EndProcedure
OpenWindow(#Window, #PB_Ignore, #PB_Ignore, 800, 600, "Native FileSystem - Demo", #PB_Window_ScreenCentered)
ButtonGadget(#LoadButton, 10, 10, 80, 30, "Load")
ButtonGadget(#SaveButton, 100, 10, 80, 30, "Save")
ButtonGadget(#SaveAsButton, 190, 10, 80, 30, "Save as...")
EditorGadget(#Editor, 10, 50, WindowWidth(#Window) - 20, WindowHeight(#Window) - 60)
BindGadgetEvent(#LoadButton, @LoadButtonEvent())
BindGadgetEvent(#SaveButton, @SaveButtonEvent())
BindGadgetEvent(#SaveAsButton, @SaveAsButtonEvent())