npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-file-manager

v1.0.9

Published

A helper utility for working with files in the browser.

Downloads

172

Readme

JSFileManager

This package makes it easier to work with files in the browser. It allows users to open files by using the standard file picker dialog, or via a drop zone. It also allows saving data to file using the standard Save As dialog.

Features:

  • Unified file interface for working with file data
  • Open files via the browser's Open dialog
  • Open files by allowing the user to drop them onto the browser window
  • Save files using the browser's Save As dialog
  • Create files using any type of data (ArrayBuffer, Blob, String, etc)

Using this package

To use this package, if you're using a packager like webpack, you can import it:

// ES6 import style
import JSFileManager, { JSFile } from 'js-file-manager'

// CommonJS style
var JSFileManager = require("js-file-manager");
var JSFile = JSFileManager.JSFile;

... or else you can just include it using the script tag:

<script src="https://npmcdn.com/js-file-manager/dist/jsfilemanager.min.js"></script>

Examples

See all tests here.

Open a file using the browser's Open dialog and read it as an array buffer:

JSFileManager.pick({ event: touchOrClickBrowserEvent })
    .then(file => file.getArrayBuffer())
	.then(arrayBuffer => ...)

Pick multiple files with a drop zone:

JSFileManager.pick({ dropzone: true, maxFiles: 999 })
    .then(files => ...)

Only accept specific file formats:

JSFileManager.pick({ accept: ".jpg, .jpeg, .gif, .png" })
    .then(file => ...)

Save an ArrayBuffer as a file on the device:

new JSFile(arrayBuffer, "Filename.ext").save()

Download a file to the device:

JSFile.fromURL("http://my.com/file.pdf").save()

JSFileManager

JSFileManager.pick(options)

Returns a promise to a JSFile object. You can pass an options object to the pick() function, with these fields:

Option | Type | Description ------------|-------------------|----------------------- event | Browser event | If you call pick() from within a trusted event and pass in the event here, the system file picker will be shown immediately. dropzone | Boolean | Defaults to false. If true, will show a drop zone overlay UI to allow the user to drop files on the browser window instead of selecting them with the picker. maxFiles | Integer | Defaults to 1. If this is higher than 1, the result of the promise will be an array of JSFiles. accept | String | File formats to accept. Can be extensions (such as ".jpg") or types (such as "image/*"). If more than one format can be accepted, separate the formats by commas (,).

JSFile

new JSFile(data, name)

Creates a new JSFile, with the specified data and name.

Argument | Type | Description ------------|-----------------------------------------------------------|----------------------- data | TypedArray, ArrayBuffer, Blob, File, String, or Object | The contents of the file. If you pass in a string, it will be saved with UTF8 encoding. If you pass in an object, it will be JSON-encoded as a string. name | String | (optional) The name of the file.

JSFile.fromURL(url, name)

Creates a new JSFile, with the contents of the specified URL.

Argument | Type | Description ------------|-----------|----------------------- url | String | The URL to the file. The file's contents are only downloaded when requested (ie. using file.getArrayBuffer() etc) name | String | (optional) The name of the file.

file.name

The file's name.

file.type

The file's mimetype.

file.size

The file's size, if available, or else 0.

file.lastModifiedDate

The file's last modified date if available, or else the date the JSFile's data was last updated.

file.save()

Saves the file. Returns a Promise once completed.

NOTE: On some browsers, this needs to be called from within a trusted event.

file.setData(data)

Update the file's data. The data won't be written to the file until you call save(). This is the same as new JSFile(data), but reuses the same file object.

file.getArrayBuffer()

Retrieves the file's contents as an ArrayBuffer. Returns a Promise.

file.getBlob()

Retrieves the file's contents as a Blob. Returns a Promise.

file.getString()

Retrieves the file's contents as a string. Returns a Promise.

file.getJSON()

Retrieves the file's contents as a JSON object. Returns a Promise.

file.getDataURL()

Retrieves the file's contents as a data URL, which can be displayed in an <img/> or other tag. The contents of the file are Base64 encoded into the URL, so this should not be used for large files. Returns a Promise.

file.getObjectURL()

Retrieves an Object URL pointing to this file's content. This is similar to getDataURL(), except the URL doesn't contain the actual data, just a pointer to it, so it works with files of any size. The URL is only valid in the context of the running web app. Returns a Promise.