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

awesome-file-input-watcher

v1.0.6

Published

A incredible way to watch file inputs and get the files when they change.

Downloads

13

Readme

Awesome File Input Watcher

Installation

If you use NPM:

npm i awesome-file-input-watcher

If you use Yarn:

yarn add awesome-file-input-watcher

Get files from a normal file input

To do this, use the watchInput() method:

import { watchInput } from 'awesome-file-input-watcher';

const input = document.querySelector('#input-id');

watchInput(input, (file) => {
  console.log(file) // The file of input when it change
});

Get files from a multiple file input

To do this, use the watchMultipleInput() method:

import { watchMultipleInput } from 'awesome-file-input-watcher';

const input = document.querySelector('#input-id');

watchMultipleInput(input, (filesArray) => {
  console.log(filesArray) // The files of input when it change
});

Get files dropped in a dropzone (like a div)

To watch the files dropped on a region of DOM (like a div) and get them after the 'drop' event, use the watchDropzone() method.

import { watchDropzone } from 'awesome-file-input-watcher';

const div = document.querySelector('div');

watchDropzone(div, (droppedFiles) => {
  console.log(droppedFiles); // The files that have been dropped
});

Get files without using inputs

To get files without using already created file inputs, use the getFileWithoutInput() function.

This method receives a function as a parameter that will be called when choosing the file and will receive the chosen file as a parameter.

import { getFileWithoutInput } from 'awesome-file-input-watcher';

const button = document.querySelector('button');

function doSomething(file) {
  console.log(file) // The file

  // ...
};

button.addEventListener('click', () => {
  getFileWithoutInput(doSomething)
});

If you want to get more than one file, you need to use the getFilesWithoutInput() method.

import { getFilesWithoutInput } from 'awesome-file-input-watcher';

const button = document.querySelector('button');

function doSomething(file) {
  console.log(file) // The file

  // ...
};

button.addEventListener('click', () => {
  getFilesWithoutInput(doSomething)
});

Warning: The file choice popup will only open if the action that activates the method was performed by the user

Use the files of a input as background image of a element

If you use the createBackgroundImageConnection() method, you can create a connection between a file input and a HTML element. When a file is added to the input, the file will be used as background image of the HTML element.

import { createBackgroundImageConnection } from 'awesome-file-input-watcher';

const input = document.querySelector('input');
const div = document.querySelector('div');

createBackgroundImageConnection(input, div);

In this example, if the input receive a image file, this image file will be used as background image of the element. And if the file change, the background image of the element will be change too.

With createBackgroundImageConnection() you can define a CSS style that will be applied to the element when the input receive a file.

import { createBackgroundImageConnection } from 'awesome-file-input-watcher';

const input = document.querySelector('input');
const div = document.querySelector('div');

createBackgroundImageConnection(input, div, {
  backgroundSize: 'cover',
  backgroundPosition: 'center',
  backgroundRepeat: 'no-repeat',
  width: '500px',
  height: '500px'
});

Fetch files located at different origin URLs

Normally, you can't download files located a different origin URLs just inserting a "download" attribute in a HTML anchor.

With the fetchDifferentOriginFile() method, you can do this just passing the URL of the content as parameter to the function.

import { fetchDifferentOriginFile } from 'awesome-file-input-watcher';

let button = document.querySelector('button');

let url = 'Some URL';

button.addEventListener('click', () => fetchDifferentOriginFile(url));

The fetchDifferentOriginFile() accepts the filename and file extension as parameters (if you don't type them, default values will be used and the file extension will be automatically generated).

The presence of the URL parameter is mandatory.

import { fetchDifferentOriginFile } from 'awesome-file-input-watcher';

let button = document.querySelector('button');

let url = 'Some URL';
let filename = 'some_filename';
let file_extension = '.jpg';

button.addEventListener('click', () => fetchDifferentOriginFile(
  url,
  filename,
  file_extension
));

Display images in a region with an image file

If you have an image file, you can display it using the readAndDisplayImage() method.

import { readAndDisplayImage } from 'awesome-file-input-watcher';

let input = document.querySelector('input');
let div = document.querySelector('div');

input.addEventListener('change', (event) => {
  let file = event.target.files[0];

  readAndDisplayImage(file, div);
});

If you want to add custom styles to the region that will receive the image as background you can pass a custom styles object as third parameter.

import { readAndDisplayImage } from 'awesome-file-input-watcher';

let input = document.querySelector('input');
let div = document.querySelector('div');

input.addEventListener('change', (event) => {
  let file = event.target.files[0];

  readAndDisplayImage(file, div, {
    backgroundPosition: 'bottom',
    backgroundSize: 'cover',
    width: '200px',
    height: '300px'
  });
});

Default styles that will be applied to background image

| Property | Value | | -------- | ----- | | background-position | center | | background-size | cover | | background-repeat | no-repeat |