Socket
Socket
Sign inDemoInstall

attr-accept

Package Overview
Dependencies
0
Maintainers
3
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    attr-accept

JavaScript implementation of the "accept" attribute for HTML5


Version published
Weekly downloads
3M
increased by4.16%
Maintainers
3
Install size
18.6 kB
Created
Weekly downloads
 

Package description

What is attr-accept?

The attr-accept package is designed to provide functionality for checking if a file meets certain criteria specified through file attributes. This is particularly useful in scenarios where file uploads need to be validated based on file type, name, or other attributes before being processed or accepted. It's commonly used in web development projects to enhance the file upload feature by filtering out unacceptable files based on their attributes.

What are attr-accept's main functionalities?

File Type Validation

This feature allows developers to validate the type of the file by specifying a MIME type pattern. In the code sample, the function `accepts` is used to check if a file named 'example.jpg' with the MIME type 'image/jpeg' matches the specified pattern 'image/*'. This is useful for ensuring that only files of a certain type are processed.

const accepts = require('attr-accept');

const file = {
  name: 'example.jpg',
  type: 'image/jpeg'
};

const accepted = accepts(file, 'image/*');
console.log(accepted); // true or false

File Extension Validation

This feature enables the validation of files based on their extension. The provided code demonstrates how to use the `accepts` function to verify if a file with the name 'example.pdf' and the type 'application/pdf' has a '.pdf' extension. This can be particularly useful for applications that only need to accept files with specific extensions.

const accepts = require('attr-accept');

const file = {
  name: 'example.pdf',
  type: 'application/pdf'
};

const accepted = accepts(file, '.pdf');
console.log(accepted); // true or false

Other packages similar to attr-accept

Readme

Source

attr-accept

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

npm version semantic-release

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

Installation

npm install --save attr-accept

Usage

var accept = require('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

Keywords

FAQs

Last updated on 09 Sep 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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc