browser-nativefs
Advanced tools
Comparing version 0.0.5 to 0.1.0
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0. | ||
export{fileOpenPromise}from"./file-open.mjs";export{fileSavePromise}from"./file-save.mjs";export{imageToBlob}from"./image-to-blob.mjs"; | ||
export{fileOpen}from"./file-open.mjs";export{fileSave}from"./file-save.mjs";export{imageToBlob}from"./image-to-blob.mjs"; |
{ | ||
"name": "browser-nativefs", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "Native File System API with legacy fallback in the browser.", | ||
@@ -5,0 +5,0 @@ "browser": "./dist/index.js", |
# Browser-NativeFS | ||
This module allows you to either use the | ||
This module allows you to easily use the | ||
[Native File System API](https://wicg.github.io/native-file-system/) on supporting browsers, | ||
with a transparent fallback to the `<input type="file">` and `<a download>` legacy methods. | ||
## Usage | ||
Read more on the background of this module in my post | ||
[Progressive Enhancement In the Age of Fugu APIs](https://blog.tomayac.com/2020/01/23/progressive-enhancement-in-the-age-of-fugu-apis/). | ||
## Usage Example | ||
The module feature-detects support for the Native File System API and | ||
@@ -13,39 +16,63 @@ only loads the actually relevant code. | ||
```js | ||
// The imported methods will use the Native File System API or a fallback implementation. | ||
import { | ||
fileOpenPromise, | ||
fileSavePromise, | ||
fileOpen, | ||
fileSave, | ||
} from 'https://unpkg.com/browser-nativefs'; | ||
(async () => { | ||
// This dynamically either loads the Native File System API | ||
// or the legacy module. | ||
const fileOpen = (await fileOpenPromise).default; | ||
const fileSave = (await fileSavePromise).default; | ||
// Open a file. | ||
const blob = await fileOpen({ | ||
mimeTypes: ['image/*'], | ||
}); | ||
// Open a file. | ||
const blob = await fileOpen({mimeTypes: ['image/*']}); | ||
// Open multiple files | ||
const blobs = await fileOpen({mimeTypes: ['image/*'], multiple: true}); | ||
// Open multiple files. | ||
const blobs = await fileOpen({ | ||
mimeTypes: ['image/*'], | ||
multiple: true, | ||
}); | ||
// Save a file. | ||
await fileSave(blob, {fileName: 'Untitled.png'}); | ||
await fileSave(blob, { | ||
fileName: 'Untitled.png', | ||
}); | ||
})(); | ||
``` | ||
## API | ||
## API Documentation | ||
Opening files: | ||
```js | ||
// Options are optional. | ||
const options = { | ||
// List of allowed MIME types, defaults to `*/*`. | ||
mimeTypes: ['image/*'], | ||
// List of allowed file extensions, defaults to `''`. | ||
extensions: ['png', 'jpg', 'jpeg', 'webp'], | ||
// Set to `true` for allowing multiple files, defaults to `false`. | ||
multiple: true, | ||
// Textual description for file dialog , defaults to `''`. | ||
description: 'Image files', | ||
}; | ||
const blobs = await fileOpen(options); | ||
``` | ||
Saving files: | ||
```js | ||
// Options are optional. | ||
const options = { | ||
// Suggested file name to use, defaults to `''`. | ||
fileName: 'Untitled.txt', | ||
}; | ||
await fileSave(someBlob, options); | ||
// Optional file handle to save back to an existing file. | ||
// This will only work with the Native File System API. | ||
// Get a `FileHandle` from the `handle` property of the `Blob` | ||
// you receive from `fileOpen()` (this is non-standard). | ||
const handle = previouslyOpenedBlob.handle; | ||
await fileSave(someBlob, options, handle); | ||
``` | ||
@@ -52,0 +79,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17401
82