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

uppie

v4.0.0

Published

Cross-browser directory and multi-file upload library

Downloads

2,981

Readme

uppie

Cross-browser file and directory and upload library

uppie is a tiny JavaScript library which helps you with file and directory uploads in browsers. It supports all current and past implementations of multi-file and directory uploads and provides you with a FormData object you can submit directly to a server through either XMLHttpRequest or fetch. Both the <input type="file"> element and drag-and-drop are supported.

Usage

npm install uppie
import {uppie} from 'uppie';

uppie(document.querySelector('#file'), async (e, formData, files) => {
  await fetch('/upload', {method: 'POST', body: formData});
});

Browser support

|| files via input[type=file] | files via DnD | directories via input[type=file] | directories via DnD | |---------|---------------------- |---------------|----------------------|--------------| | Firefox | yes | yes | yes (50+) | yes (50+) | | Chrome | yes | yes | yes (29+) | yes (29+) | | Edge | yes | yes | yes (13+) | yes (14+) | | Safari | yes | yes | yes (11.1+) | yes (11.1+) |

Notes

  • Empty directories are excluded from the results by all browsers as dictated by the spec.
  • Firefox and Safari exclude files and directories starting with a ..

API

uppie(node, [opts], callback)

  • node Node or NodeList: One or more DOM nodes. If a <input type="file"> is given, uppie will monitor it for change events. Any other element type will be enabled as a dropzone and watched for drop events. If you want to use both on the same element, use a hidden <input> and forward the click event.
  • opts Object: A options object which can contain:
    • name: The name attribute for creating the FormData entries. Default: "files[]".
  • callback Function: callback which is called every time the selected files change or when files are dropped in the dropzone.

The callback receives

  • event Event: the original event. Useful for calling event.stopPropagation().
  • formData FormData: FormData object to be used for XHR2 uploading.
  • files Array: Array of paths for preview purposes.

FormData format

name defaults to "files[]", filename will be the full path to the file, with / used as path separator. Does not include a leading slash. Make sure to sanitize filename on the server before writing it to the disk to prevent exploits involving .. in the path. Example FormData:

------Boundary
Content-Disposition: form-data; name="files[]"; filename="docs/1.txt"
Content-Type: text/plain

[DATA]
------Boundary
Content-Disposition: form-data; name="files[]"; filename="docs/path/2.txt"
Content-Type: text/plain

[DATA]
------Boundary
Content-Disposition: form-data; name="files[]"; filename="docs/path/to/3.txt"
Content-Type: text/plain

Recommended input element attributes

  • multiple: allow multiple files or directories to be selected.
  • webkitdirectory: enable directory uploads.

PHP example

Below is example for PHP 7.0 and possibly earlier versions. PHP does not parse the path from the filename field, so it is necessary to submit the path through other means, like as separate FormData fields as done in the example.

import {uppie} from 'uppie';

const uppie = new Uppie();

uppie(document.documentElement, (event, formData, files) => {
  files.forEach(path => {
    formData.append("paths[]", path);
  });

  const xhr = new XMLHttpRequest();
  xhr.open('POST', 'upload.php');
  xhr.send(formData);
});

And in upload.php:

foreach ($_FILES['files']['name'] as $i => $name) {
  if (strlen($_FILES['files']['name'][$i]) > 1) {
    $fullpath = strip_tags($_POST['paths'][$i]);
    $path = dirname($fullpath);

    if (!is_dir('uploads/'.$path)){
      mkdir('uploads/'.$path);
    }
    if (move_uploaded_file($_FILES['files']['tmp_name'][$i], 'uploads/'.$fullpath)) {
        echo '<li>'.$name.'</li>';
    }
  }
}

Note that PHP's upload limits might need to be raised depending on use case.

© silverwind, distributed under BSD licence