How to get file content and other details in AngularJS?

14 Aug, 2020
We can get the file content by using some basic angular functions and other details like the name and size of the file in AngularJS. To understand it look into the below example where both HTML and JS files are implemented.
Note: Consider below two files are of same component in angular.
app.module.html:
<!-- Script for display data in particular format -->  <!DOCTYPE html> <html><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">  </script> <body ng-app="myApp">    <div ng-controller="MyCtrl">        <input type="file" id="myFileInput" />        <button ng-click="submit()"> Submit</button>        <br /><br />        <h1>            Filename: {{ fileName }}        </h1>        <h2>            File size: {{ fileSize }} Bytes        </h2>        <h2>            File Content: {{ fileContent }}        </h2>    </div></body></html>
Output:
In the above HTML file we have simply made a structure to how it should be looked on the webpage. For that, we have used some angular stuff like ‘ng-controller’ and also doubly curly brackets which we will implement in the below javascript code.
app.module.ts:
import { BrowserModule } from         '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { BrowserAnimationsModule } from         '@angular/platform-browser/animations'; import { FormsModule, ReactiveFormsModule }         from '@angular/forms'; import { MatInputModule } from         '@angular/material/input'; import { MatDialogModule } from         '@angular/material/dialog'; import { MatFormFieldModule } from         '@angular/material/form-field'; import { MatIconModule } from         '@angular/material/icon';   @NgModule({     declarations: [         AppComponent,     ],     imports: [         BrowserModule,         FormsModule,         BrowserAnimationsModule,         MatInputModule,         MatFormFieldModule,         MatIconModule,         MatDialogModule,     ],     bootstrap: [AppComponent] }) export class AppModule { } 
app.component.ts:
// Code to get file content// and other dataimport { Component, OnInit }          from '@angular/core';     // Imports import { FormGroup, FormControl,          } from '@angular/forms';  @Component({     selector: 'app-root',     templateUrl: './app.component.html',     styleUrls: ['./app.component.scss'] }) export class AppComponent implements OnInit {         constructor() { }         ngOnInit() {     }      var myApp = angular.module('myApp', []);      myApp.controller('MyCtrl', function ($scope) {          // Intially declaring empty string         // and assigning size to zero           $scope.fileContent = '';        $scope.fileSize = 0;        $scope.fileName = '';          // Implementing submit function           $scope.submit = function () {            var file = document.getElementById("myFileInput")                                          .files[0];            if(file) {                var Reader = new FileReader();                Reader.readAsText(file, "UTF-8");                Reader.onload = function (evt) {                      // Getting required result                     // of the file                      $scope.fileContent = Reader.result;                    $scope.fileName = document.getElementById(                                     "myFileInput").files[0].name;                    $scope.fileSize = document.getElementById(                                      "myFileInput").files[0].size;;                }         // Printing error if data        //is not proper               Reader.onerror = function (evt) {                $scope.fileContent = "error";            }          }       }    }});
Output:
AngularJS-Misc
Picked
AngularJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Auth Guards in Angular 9/10/11
Routing in Angular 9/10
How to bundle an Angular app for production?
What is AOT and JIT Compiler in Angular ?
Angular PrimeNG Dropdown Component
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?