• Stars
    star
    447
  • Rank 97,700 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🖼 Simple file-upload utility that shows a preview of the uploaded image. Written in TypeScript. No dependencies. Works well with or without a framework.

file-upload-with-preview

🖼 Simple file-upload utility that shows a preview of the uploaded image. Written in TypeScript. No dependencies. Works well with or without a framework.

NPM Version NPM Downloads License Tweet

Links

Install

yarn add file-upload-with-preview

Or, you can include it through the browser.

<link
  rel="stylesheet"
  type="text/css"
  href="https://unpkg.com/file-upload-with-preview/dist/style.css"
/>

<script src="https://unpkg.com/file-upload-with-preview/dist/file-upload-with-preview.iife.js"></script>

About

This is a simple frontend utility meant to help the file-upload process on your website.

It is written in pure JavaScript using TypeScript and has no dependencies. You can check out the live demo here.

For the most part, browsers do a good job of handling image-uploads. That being said - I find the ability to show our users a preview of their upload can go a long way in increasing the confidence in their upload.

file-upload-with-preview aims to address the issue of showing a preview of a user's uploaded image in a simple to use package.

Features

  • Shows the actual image preview in the case of a single uploaded .jpg, .jpeg, .gif, or .png image. Shows a success-image in the case of an uploaded .pdf file, uploaded video, or other un-renderable file - so the user knows their image was collected successfully. In the case of multiple selected files, the user's selected images will be shown in a grid.
  • Shows the image name in the input bar. Shows the count of selected images in the case of multiple selections within the same input.
  • Allows the user to clear their upload and clear individual images in the multiple grid
  • Looks great
  • Framework agnostic - to access the uploaded file/files just use the cachedFileArray (always will be an array) property of your FileUploadWithPreview object.
  • For every file-group you want just initialize another FileUploadWithPreview object with its own uniqueId - this way you can have multiple file-uploads on the same page. You also can just use a single input designated with a multiple property to allow multiple files on the same input.

Usage

This library looks for a specific HTML element to display the file-upload. Simply add the below div to your HTML. Make sure to populate your unique id in the data-upload-id attribute.

<div class="custom-file-container" data-upload-id="my-unique-id"></div>

Then, initialize your file-upload in the JavaScript like below:

import { FileUploadWithPreview } from 'file-upload-with-preview';
import 'file-upload-with-preview/dist/style.css';

const upload = new FileUploadWithPreview('my-unique-id');

If you're importing directly in the browser, use the following instead:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <link
      rel="stylesheet"
      type="text/css"
      href="https://unpkg.com/file-upload-with-preview/dist/style.css"
    />
  </head>
  <body>
    <div class="custom-file-container" data-upload-id="my-unique-id"></div>
    <script src="https://unpkg.com/file-upload-with-preview/dist/file-upload-with-preview.iife.js"></script>
  </body>
</html>

Then initialize like this:

const upload = new FileUploadWithPreview.FileUploadWithPreview('my-unique-id');

Then when you're ready to use the user's file for an API call or whatever, just access the user's uploaded file/files in the cachedFileArray property of your initialized object like this:

upload.cachedFileArray;

You can optionally trigger the image browser and clear selected images programmatically. There are additional methods on the class if you'd like to take a look at the source code.

upload.emulateInputSelection(); // to open image browser
upload.resetPreviewPanel(); // clear all selected images

You may also want to capture the event when an image is selected.

import { Events, ImageAddedEvent } from 'file-upload-with-preview';

window.addEventListener(Events.IMAGE_ADDED, (e: Event) => {
  const { detail } = e as unknown as ImageAddedEvent;

  console.log('detail', detail);
});

Note

The cachedFileArray property is always an array. So if you are only allowing the user to upload a single file, you can access that file at cachedFileArray[0] - otherwise just send the entire array to your backend to handle it normally.

Make sure to pass in multiple: true in your options if you want to allow the user to select multiple images.

Docs

View the full docs here.

Full Example

See the full example in the ./example/index.ts folder. See the top of this README for some links to a few live CodeSandbox's.

Browser Support

If you are supporting a browser like IE11, one way to add a polyfill for fetch and promise is by adding the following in the bottom of your index.html:

<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.js"></script>

Development

# Install dependencies
yarn

# Watch changes during local development
yarn dev

# Run tests
yarn test

# Build library
yarn build

Other

Go ahead and fork the project! Submit an issue if needed. Have fun!

License

MIT

More Repositories

1

koa-vue-notes-api

🤓 This is a simple SPA built using Koa as the backend, Vue as the first frontend, and React as the second frontend. Features MySQL integration, user authentication, CRUD note actions, and async/await.
JavaScript
366
star
2

vue-simple-context-menu

📌 Simple context-menu component built for Vue. Works well with both left and right clicks. Nothing too fancy, just works and is simple to use.
Vue
220
star
3

koa-vue-notes-web

🤓 This is a simple SPA built using Koa as the backend, Vue as the first frontend, and React as the second frontend. Features MySQL integration, user authentication, CRUD note actions, and Vuex store modules.
SCSS
199
star
4

vue-navigation-bar

🧭 A simple, pretty navbar for your Vue projects.
Vue
165
star
5

vue-cookie-accept-decline

👋 Show a banner with text, a decline button, and an accept button on your page. Remembers selection using cookies. Emits an event with current selection and on creation. Good for GDPR requirements.
Vue
137
star
6

koa-react-notes-web

🤓 This is a simple SPA built using Koa as the backend, Vue as the first frontend, and React as the second frontend. Features MySQL integration, user authentication, CRUD note actions, and more.
TypeScript
68
star
7

vue-screen-size

🖥 Get easy and reactive access to the width and height of your screen.
Vue
43
star
8

modern-webpack-starter

🏰 A modern JavaScript starter using Webpack 4. Made in a simple way - good for learning or starting a new project without having to rollout cli-auto-builders.
JavaScript
41
star
9

vue-video-section

📼 A simple video header/section component for Vue. Good for video backgrounds and overlaying content on them.
Vue
32
star
10

vue-programmatic-invisible-google-recaptcha

🔒A simple invisible Google reCAPTCHA component focused solely on programmatic invocation.
Vue
27
star
11

vue-countable

✍️ Vue binding for countable.js. Provides real-time paragraph, sentence, word, and character counting.
Vue
25
star
12

koa-vue-notes-project

🤓 A single repo containing both the koa-vue-notes-api and koa-vue-notes-web projects.
JavaScript
14
star
13

chrome-ribbon-reminder

🎀 A Chrome extension written using Vue and Async/Await. Uses a popup display and changes badge counts.
Vue
12
star
14

vue-mock-layout

💻 Easily mock the layout of your Vue apps.
Vue
7
star
15

vue-mailchimp-email-signup-form

📮 Easily collect email signups using MailChimp in your Vue projects.
JavaScript
4
star
16

react-mailchimp-email-signup-form

📮 Easily collect email signups using MailChimp in your React projects.
TypeScript
3
star
17

find-the-state-ui

🔍 Find all the States in the contiguous USA on a map. Simple enough.
TypeScript
1
star
18

find-the-state-api

🔍 Find all the States in the contiguous USA on a map. Simple enough.
Python
1
star
19

RecordPlayer

🎻 Emulate a record player in Sprite Kit using Swift and Xcode.
Swift
1
star