Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • AngularJS Tutorial
  • AngularJS Directives
  • AngularJS Functions
  • AngularJS Filters
  • AngularJS Examples
  • AngularJS Interview Questions
  • Angular ngx Bootstrap
  • AngularJS Cheat Sheet
  • AngularJS PrimeNG
  • JavaScript
  • Web Technology
Open In App
Next Article:
Angular PrimeNG FileUpload Events
Next article icon

Angular File Upload

Last Updated : 09 Jan, 2025
Comments
Improve
Suggest changes
Like Article
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:

  1. Create a new angular app using following command-
    ng new angular-file-upload 
  2. Move inside the app by using cd command-
    cd src/app/
  3. Generate new component file-upload-
    ng g c file-upload/ 
  4. Open src/app folder and start editing app.component.html file. html
    <app-file-upload></app-file-upload> 
  5. Create a service for file-upload component via command-
    ng g s file-upload/
  6. 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                  }             }         );     } } 
  7. 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://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)   } } 
  8. 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> 
  9. 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 { } 
  10. Now run this command to serve on localhost
    ng serve
  11. Output:
    • Before file selection:
    • After file selection and clicking on button:
    • After loading is completed:
  12. 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.


Next Article
Angular PrimeNG FileUpload Events

R

Ritik_Dua
Improve
Article Tags :
  • AngularJS
  • HTML
  • JavaScript
  • Web Technologies
  • AngularJS-Misc
  • HTML-Misc

Similar Reads

  • Blaze UI File Upload
    Blaze UI is a framework-free open source UI toolkit that uses JavaScript components to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website look more amazing. This framework allows us to use various of its styles and properti
    2 min read
  • Angular PrimeNG FileUpload Basic
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use FileUpload Basic in Angular PrimeNG. Angular PrimeNG FileUpl
    3 min read
  • Angular PrimeNG FileUpload File Size
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use FileUpload File Size in Angular PrimeNG. The FileUpload Compone
    3 min read
  • Angular PrimeNG FileUpload Events
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn about Angular PrimeNG FileUpload Events The FileUpload component is u
    3 min read
  • Angular PrimeNG FileUpload File Types
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use FileUpload File Types in Angular PrimeNG. The FileUpload Compon
    3 min read
  • Angular PrimeNG FileUpload Styling
    A responsive website may be created with great ease using the open-source Angular PrimeNG framework, which has a wide range of native Angular UI components for superb style. In this article, we are going to discuss Angular PrimeNG FileUpload Styling.  A powerful uploader, FileUpload supports drag-an
    3 min read
  • Angular PrimeNG FileUpload Advanced
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use FileUpload Advanced in Angular PrimeNG. Angular PrimeNG File
    3 min read
  • Angular PrimeNG FileUpload Basic UI
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use FileUpload Basic UI in Angular PrimeNG. Angular PrimeNG FileUpl
    3 min read
  • Angular PrimeNG FileUpload DragDrop
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use the FileUpload DragDrop in Angular PrimeNG. Angular PrimeNG
    3 min read
  • Angular PrimeNG FileUpload Auto Uploads
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use the FileUpload Auto Uploads in Angular PrimeNG. Angular Prim
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences