GitHub

JavaScript implementation of the "accept" attribute for HTML5 <input type="file">

npm Tests coverage

See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#attr-accept for more information.

Ships as both ES modules and CommonJS with bundled TypeScript type definitions.

Installation

npm install --save attr-accept

Usage

import accept from "attr-accept";
accept(
  {
    name: "my file.png",
    type: "image/png"
  },
  "image/*"
); // => true
accept(
  {
    name: "my file.json",
    type: "application/json"
  },
  "image/*"
); // => false
accept(
  {
    name: "my file.srt",
    type: ""
  },
  ".srt"
); // => true

You can also pass multiple mime types as a comma delimited string or array.

accept(
  {
    name: "my file.json",
    type: "application/json"
  },
  "application/json,video/*"
); // => true
accept(
  {
    name: "my file.json",
    type: "application/json"
  },
  ["application/json", "video/*"]
); // => true

In a CommonJS environment, use require instead:

const accept = require("attr-accept");

Contributing

Checkout the organization CONTRIBUTING.md.

Read the original on github.com ↗