Giter Site home page Giter Site logo

sshyran / browser-nativefs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlechromelabs/browser-fs-access

0.0 2.0 0.0 90 KB

Native File System API with legacy fallback in the browser

Home Page: https://browser-nativefs.glitch.me/

License: Apache License 2.0

Shell 3.86% JavaScript 96.14%

browser-nativefs's Introduction

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. This library is a ponyfill.

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

Live Demo

See the library in action: https://browser-nativefs.glitch.me/.

Origin Trial

โš ๏ธ In order to use the Native File System API, you need to quickly request an origin trial token for your application and communicate it to the browser via a meta tag or an HTTP header.

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,
  directoryOpen,
  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,
  });

  // Open all files in a directory,
  // recursively including subdirectories.
  const blobsInDirectory = await directoryOpen({
    recursive: true,
  });

  // Save a file.
  await fileSave(blob, {
    fileName: 'Untitled.png',
    extensions: ['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);

Opening directories:

// Options are optional.
const options = {
  // Set to `true` to recursively open files in all subdirectories,
  // defaults to `false`.
  recursive: true,
};

const blobs = await directoryOpen(options);

The module also polyfills a webkitRelativePath property on returned files in a consistent way, regardless of the underlying implementation.

Saving files:

// Options are optional.
const options = {
  // Suggested file name to use, defaults to `''`.
  fileName: 'Untitled.txt',
  // Suggested file extensions, defaults to `''`.
  extensions: ['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

Alternatives

A similar, but more extensive library called native-file-system-adapter is provided by @jimmywarting.

Acknowledgements

Thanks to @developit for improving the dynamic module loading and @dwelle for the helpful feedback and issue reports. Directory operations were made consistent regarding webkitRelativePath and parallelized and sped up significantly by @RReverser. The TypeScript type annotations were provided by @nanaian.

License and Note

Apache 2.0.

This is not an official Google product.

browser-nativefs's People

Contributors

tomayac avatar developit avatar bates64 avatar rreverser avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.