GithubHelp home page GithubHelp logo

rawatanimesh / angular-file-reader Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 173 KB

Light weight file reader angular component without external libraries/dependencies

Home Page: https://file-reader.stackblitz.io

JavaScript 14.02% TypeScript 76.79% HTML 5.33% CSS 3.85%
file-reader reader angular typescript filesystem

angular-file-reader's Introduction

File Reader

Light weight file reader angular component without external libraries/dependencies.

alt text

Demo

Checkout the demo on StackBlitz - https://file-reader.stackblitz.io

Features

1) Read contents of file from local system
2) Apply validations to read only specific file types. Example - '.txt' or any custom file extension
3) Directly parse JSON data from using file-reader

Adding the component in your project

Add Component in module

Import import { FileReaderComponent } from './file-reader/file-reader.component';

Declaration declarations: [ FileReaderComponent ]

Add selector in HTML

<file-reader (onFileRead)="readContents($event)"></file-reader>

Selector Events

Event onFileRead will return the contents of the file selected by user.

file-reader.component.ts

import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'file-reader',
  templateUrl: './file-reader.component.html',
  styleUrls: ['./file-reader.component.scss']
})
export class FileReaderComponent {
  file: any;
  tempFileData: any;
  @Output() onFileRead:EventEmitter<any> = new EventEmitter();
  openProject() {
    document.getElementById('my_file').click();
  }
  fileSelection(event) {
    this.file = event.target.files[0];
    console.log(this.file.name);
    //we can change these validations of file type as per our requirement
    if (this.file.name.split('.').pop() == 'txt') {
      let fileReader = new FileReader();
      fileReader.onload = (e) => {
        this.tempFileData = fileReader.result;
        this.onFileRead.emit(this.tempFileData);
      }
      fileReader.readAsBinaryString(this.file);
    }
    else {
      alert("Please choose a .txt file.");
      this.tempFileData = '';
      this.onFileRead.emit(this.tempFileData);
    }
  }
}

file-reader.component.html

<div class="file-container">
  <button class="file-button" (click)="openProject()">Open File</button>
  <input type="file" id="my_file" (change)="fileSelection($event)" class="file_exp_opener">
  <br>
</div>

file-reader.component.scss

.file-container{
    text-align: center;
  }
.file-button{
    outline: none;
    cursor: pointer;
    border: 1px solid #007bff;
    border-radius: 5px;
    background: #007bff;
    color: #fff;
    font-size: 15px;
    padding: 10px;
    margin: 5px 10px;
    min-width: 100px;
  }
.file-button:hover{
    background: #0069d9;
    border: 1px solid #0062cc;
  }
#my_file{
    display: none;
  }

Author

[email protected]

angular-file-reader's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.