Abstract away loading and saving of text files in the browser. The given save and load options are "best effort". If the new File System Access API is not available in the browser, it will still work, but basically ignore all your preferences.
- Source:
Example
import files from "save-load-file.js";
await files.save({
suggestedName: "Untitled.extension",
contents: "I'm in your files",
startIn: "documents",
types: [{
description: 'My very special file type',
accept: {
'text/plain': ['.extension'],
},
}],
});
await files.load({
startIn: "documents",
types: [{
description: 'My very special file type',
accept: {
'text/plain': ['.extension'],
},
}],
});
Methods
(static) load(options) → {string}
Get an upload from the user ("Open file")
Parameters:
Name | Type | Description |
---|---|---|
options |
LoadOptions | What to open, and from where |
- Source:
Returns:
The contents of the opened file
- Type
- string
(static) save(options)
Push a download to the user ("Save as")
Parameters:
Name | Type | Description |
---|---|---|
options |
SaveOptions | What to store, and where |
- Source:
Type Definitions
FileTypes
See types here: https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
description |
string | What these files are known as |
accept |
Object | The accepted files types and associated extensions |
- Source:
LoadOptions
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
types |
Array.<FileTypes> | Which file types are supported |
startIn |
"desktop" | "document" | "downloads" | "music" | "pictures" | "videos" | Where the file picker opens |
- Source:
SaveOptions
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
suggestedName |
string | The name to save the file as |
contents |
string | What should be stored in the file |
types |
Array.<FileTypes> | Which file types are supported |
startIn |
"desktop" | "document" | "downloads" | "music" | "pictures" | "videos" | Where the file picker opens |
- Source: