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

ng5-files

v1.0.6

Published

Angular5 files upload by click or/and drag and drop

Downloads

5

Readme

License: MIT npm version Build Status

ng5-files

Upload files by clicking or dragging

Getting started

npm i --save ng5-files

Add following lines into your

module:

import { Ng5FilesModule } from './ng5-files';

add Ng5FilesModule to your module imports section

imports: [ Ng5FilesModule ]

component template:

Upload by click:

<ng5-files-click (filesSelect)="filesSelect($event)">
  <span>Click me to upload</span>
</ng5-files-click>

Upload with drag'n'drop:

<ng5-files-drop (filesSelect)="filesSelect($event)">
  <div style="display: inline-block; height: 100px; width: 100px; background-color: gray">
    {{selectedFiles}}
  </div>
</ng5-files-drop>

component ts:

import {
  Ng5FilesStatus,
  Ng5FilesSelected
} from './ng5-files';
 
...
 
public selectedFiles;
 
public filesSelect(selectedFiles: Ng5FilesSelected): void {
    if (selectedFiles.status !== Ng5FilesStatus.STATUS_SUCCESS) {
      this.selectedFiles = selectedFiles.status;
      return;
      
      // Hnadle error statuses here
    }

    this.selectedFiles = Array.from(selectedFiles.files).map(file => file.name);
  }

##Configure

To pass config to ng5-files add following lines to you component.ts file:

Shared Config

import {
  Ng5FilesService,
  Ng5FilesConfig,
} from './ng5-files';
 
...
 
constructor(
      private ng5FilesService: Ng5FilesService
  ) {}
 
private testConfig: Ng5FilesConfig = {
    acceptExtensions: ['js', 'doc', 'mp4'],
    maxFilesCount: 5,
    maxFileSize: 5120000,
    totalFilesSize: 10120000
  };
   
ngOnInit() {
    this.ng5FilesService.addConfig(this.testConfig);
}

Private configs

Config added this way this.ng5FilesService.addConfig(this.testConfig); is shared config. All components will use it.

But you can add multiple configs for your upload components. Let's say, you have two upload components and you want to allow user upload just one video and 5(max) images. To do this create 2 configs and pass it to upload components as named configs.

.ts

import {
  Ng5FilesService,
  Ng5FilesConfig,
  Ng5FilesStatus,
  Ng5FilesSelected
} from './ng5-files';
 
 ...
 
public selectedFiles; 
 
private configImage: Ng5FilesConfig = {
    acceptExtensions: ['jpg', 'jpeg'],
    maxFilesCount: 5,
    totalFilesSize: 101200000
  };
  
private configVideo: Ng5FilesConfig = {
    acceptExtensions: ['mp4', 'avi'],
    maxFilesCount: 1
  };  
 
constructor(
      private ng5FilesService: Ng5FilesService
  ) {}

  ngOnInit() {
    this.ng5FilesService.addConfig(this.configImage, 'my-image-config');
    this.ng5FilesService.addConfig(this.configVideo, 'my-video-config');
  }

  public filesSelect(selectedFiles: Ng5FilesSelected): void {
    if (selectedFiles.status !== Ng5FilesStatus.STATUS_SUCCESS) {
      this.selectedFiles = selectedFiles.status;
      return;
    }
 
    // Handle error statuses here
 
    this.selectedFiles = Array.from(selectedFiles.files).map(file => file.name);
  } 
 

.html

<ng5-files-click (filesSelect)="filesSelect($event)" [configId]="'my-image-config'">
  <span>Upload</span>
</ng5-files-click>
 

<ng5-files-drop (filesSelect)="filesSelect($event)" [configId]="'my-video-config'">
  <div style="display: inline-block; height: 100px; width: 100px; background-color: gray">
    {{selectedFiles}}
  </div>
</ng5-files-drop>

API

Config

acceptExtensions values: string[] or '*' examples: ['ts', 'spec.ts'], ['js'], '*'

maxFilesCount: values: [number]

maxFileSize: values: [number] (bytes)

totalFilesSize: values: [number] (bytes)

Template

<ng5-files-click (filesSelect)="YOUR_HANDLER($event)" [configId]="YOUR_CONFIG">

filesSelect emit when files attached and pass Ng5FilesSelected object to YOUR_HANDLER:

export enum Ng5FilesStatus {
    STATUS_SUCCESS,
    STATUS_MAX_FILES_COUNT_EXCEED,
    STATUS_MAX_FILE_SIZE_EXCEED,
    STATUS_MAX_FILES_TOTAL_SIZE_EXCEED,
    STATUS_NOT_MATCH_EXTENSIONS
}

export interface Ng5FilesSelected {
  status: Ng5FilesStatus;
  files: File[];
}

! Note on statuses STATUS_MAX_FILE_SIZE_EXCEED or STATUS_NOT_MATCH_EXTENSIONS you get files not passed validation, so you shouldn't filter it manually to find all invalid files.

configId Pass your named config with configId

Caveat

Please don't use button tag in template inside ng5-files-click Don't: html <ng5-files-click> <button></button> </ng5-files-click> ng5-files-click content is wrapped in label tag, so prefer something like

<ng5-files-click>
    <span role="button" style="btn">Give me file ^.^</button>
</ng5-files-click>```