Socket
Socket
Sign inDemoInstall

browser-nativefs

Package Overview
Dependencies
0
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    browser-nativefs

Native File System API with legacy fallback in the browser.


Version published
Weekly downloads
178
increased by14.1%
Maintainers
1
Install size
17.4 kB
Created
Weekly downloads
 

Readme

Source

Browser-NativeFS

This module allows you to easily use the Native File System API on supporting browsers, with a transparent fallback to the <input type="file"> and <a download> legacy methods.

Read more on the background of this module in my post Progressive Enhancement In the Age of Fugu APIs.

Usage Example

The module feature-detects support for the Native File System API and only loads the actually relevant code.

// The imported methods will use the Native
// File System API or a fallback implementation.
import {
  fileOpen,
  fileSave,
} from 'https://unpkg.com/browser-nativefs';

(async () => {
  // Open a file.
  const blob = await fileOpen({
    mimeTypes: ['image/*'],
  });

  // Open multiple files.
  const blobs = await fileOpen({
    mimeTypes: ['image/*'],
    multiple: true,
  });

  // Save a file.
  await fileSave(blob, {
    fileName: 'Untitled.png',
  });
})();

API Documentation

Opening files:

// 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:

// Options are optional.
const options = {
   // Suggested file name to use, defaults to `''`.
  fileName: 'Untitled.txt',
};

// 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);

Browser-NativeFS in Action

You can see the module in action in the Excalidraw drawing app.

excalidraw

Acknowledgements

Thanks to @developit for improving the dynamic module loading and @dwelle for the helpful feedback and issue reports.

License and Note

Apache 2.0.

This is not an official Google product.

Keywords

FAQs

Last updated on 12 Feb 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc