Angular File Upload Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 19 Likes Like Report The file upload is an essential component to make a form that store some image kind of data. It helps in applications using image upload or in the file sharing. This file-upload component uses file.io API for uploading file and in return it provides a shareable link. Furthermore, we can send get request to shareable link to get the file but for now, our only focus is on upload section so we only use the post method. Approach:Create a new angular app using following command- ng new angular-file-upload Move inside the app by using cd command- cd src/app/Generate new component file-upload- ng g c file-upload/ Open src/app folder and start editing app.component.html file. html <app-file-upload></app-file-upload> Create a service for file-upload component via command- ng g s file-upload/Open src/app/file-upload folder and start editing file-upload.component.ts file. javascript import { Component, OnInit } from '@angular/core'; import { FileUploadService } from './file-upload.service'; @Component({ selector: 'app-file-upload', templateUrl: './file-upload.component.html', styleUrls: ['./file-upload.component.css'] }) export class FileUploadComponent implements OnInit { // Variable to store shortLink from api response shortLink: string = ""; loading: boolean = false; // Flag variable file: File = null; // Variable to store file // Inject service constructor(private fileUploadService: FileUploadService) { } ngOnInit(): void { } // On file Select onChange(event) { this.file = event.target.files[0]; } // OnClick of button Upload onUpload() { this.loading = !this.loading; console.log(this.file); this.fileUploadService.upload(this.file).subscribe( (event: any) => { if (typeof (event) === 'object') { // Short link via api response this.shortLink = event.link; this.loading = false; // Flag variable } } ); } } Open src/app/file-upload/ and start editing file-upload.service.ts file. javascript import { Injectable } from '@angular/core'; import {HttpClient} from '@angular/common/http'; import {Observable} from 'rxjs'; @Injectable({ providedIn: 'root' }) export class FileUploadService { // API url baseApiUrl = "https://www.file.io/" constructor(private http:HttpClient) { } // Returns an observable upload(file):Observable<any> { // Create form data const formData = new FormData(); // Store form name as "file" with file data formData.append("file", file, file.name); // Make http post request over api // with formData as req return this.http.post(this.baseApiUrl, formData) } } Open src/app/file-upload and start editing file-upload.component.html file. html <div class="text-center"> <input class="form-control" type="file" (change)="onChange($event)"> <button (click)="onUpload()" class="btn btn-success"> Upload </button> </div> <!-- Shareable short link of uploaded file --> <div class="container text-center jumbotron" *ngIf="shortLink"> <h2> Visit Here</h2> <a href="{{shortLink}}">{{shortLink}}</a> </div> <!--Flag variable is used here--> <div class="container" *ngIf="loading"> <h3>Loading ...</h3> </div> Open src/app/ and start editing app.module.ts file. javascript import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FileUploadComponent } from './file-upload/file-upload.component'; import { AppComponent } from './app.component'; import {HttpClientModule} from '@angular/common/http'; @NgModule({ declarations: [ AppComponent, FileUploadComponent, ], imports: [ BrowserModule, HttpClientModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { } Now run this command to serve on localhost ng serveOutput:Before file selection:After file selection and clicking on button:After loading is completed:HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples. Create Quiz Comment R Ritik_Dua Follow 19 Improve R Ritik_Dua Follow 19 Improve Article Tags : AngularJS HTML-Misc AngularJS-Misc Explore AngularJS BasicsAngularJS Tutorial 5 min read Introduction to AngularJS 4 min read Angular CLI | Angular Project Setup 3 min read AngularJS Expressions 2 min read AngularJS Modules 3 min read AngularJS ng-model Directive 4 min read AngularJS Data Binding 4 min read AngularJS Controllers 3 min read AngularJS | Scope 2 min read AngularJS Services 4 min read AngularJS | AJAX - $http 3 min read AngularJS | Tables 2 min read AngularJS Select Boxes 2 min read AngularJS SQL 3 min read AngularJS HTML DOM 2 min read AngularJS Events 3 min read AngularJS | Forms 3 min read AngularJS Form Validation 3 min read AngularJS | API 2 min read AngularJS and W3.CSS 2 min read AngularJS Includes 3 min read AngularJS Animations 1 min read AngularJS | Application 3 min read AngularJS DirectivesAngularJS Directives 9 min read AngularJS ng-app Directive 1 min read AngularJS ng-bind Directive 2 min read AngularJS ng-bind-html Directive 2 min read AngularJS ng-bind-template Directive 2 min read AngularJS ng-blur Directive 1 min read AngularJS ng-change Directive 2 min read AngularJS ng-checked Directive 2 min read AngularJS ng-class Directive 2 min read AngularJS ng-class-even Directive 2 min read AngularJS ng-class-odd Directive 2 min read AngularJS ng-click Directive 2 min read AngularJS ng-cloak Directive 2 min read AngularJS ng-controller Directive 2 min read AngularJS Directives Complete Reference 2 min read AngularJS FiltersAngularJS | Filters 7 min read AngularJS currency Filter 2 min read AngularJS | date Filter 2 min read AngularJS filter Filter 3 min read AngularJS json Filter 2 min read AngularJS limitTo Filter 2 min read AngularJS lowercase Filter 1 min read AngularJS number Filter 1 min read AngularJS orderBy Filter 4 min read AngularJs uppercase Filter 1 min read AngularJS Converting FunctionsAngularJS angular.lowercase() Function 2 min read AngularJS angular.uppercase() Function 1 min read AngularJS angular.forEach() Function 1 min read AngularJS Comparing FunctionsAngularJS angular.isArray() Function 2 min read AngularJS angular.isDate() Function 2 min read AngularJS angular.isDefined() Function 2 min read AngularJS angular.isElement() Function 2 min read AngularJS angular.isFunction() Function 2 min read AngularJS angular.isNumber() Function 2 min read AngularJS angular.isObject() Function 2 min read AngularJS | angular.isString() Function 1 min read AngularJS angular.isUndefined() Function 2 min read AngularJS angular.equals() Function 2 min read AngularJS angular.toJson() Function 2 min read AngularJS QuestionsHow to bundle an Angular app for production? 4 min read How to add many functions in one ng-click directive? 2 min read How to directly update a field by using ng-click in AngularJS ? 3 min read How to Add Dynamic Options for Multiple Selects Inside ng-repeat Directive ? 3 min read How to detect when an @Input() value changes in Angular? 3 min read How to open popup using Angular and Bootstrap ? 2 min read How to reload or re-render the entire page using AngularJS? 2 min read How to add input fields dynamically on button click in AngularJS ? 2 min read How to Create Button Dynamically with Click Event in Angular ? 2 min read How to use jQuery in Angular ? 2 min read AngularJS Examples 2 min read Like