Open In App

Angular PrimeNG FileUpload Templates

Last Updated : 31 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 know how to use FileUpload Templates in Angular PrimeNG.

The FileUpload Component is used to make an element that provides users to upload file content.

This article will show the FileUpload Templating in Angular PrimeNG. The templates are used to put some content on some pre-structured containers. 

 

FileUpload templates are discussed below:

  • file: It is used to customize the UI to display a file uploaded in the FileUpload.
  • content: It is used to customize the content of the FileUpload.
  • toolbar: It is used to customize the toolbar of the FileUpload.

Creating Angular application & module installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

Steps to run the application: Run the below command to see the output

ng serve --save

Example 1: Below is the example code that illustrates the use of Angular PrimeNG FileUpload Templates using pTemplate=”toolbar”.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG FileUpload Templates</h5>
  
<p-fileUpload 
    name="geeks[]" url="./upload.php" 
    multiple="multiple" accept=".txt" 
    maxFileSize="1000000">
    <ng-template pTemplate="toolbar">
        <h5 style="color: red">
              Hey Geek! please select files
          </h5>
    </ng-template>
</p-fileUpload>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    uploadedToGfG: any[] = [];
  
    onUpload(event) {
        for (let item of event.items) {
            this.uploadedToGfG.push(item);
        }
    }
}


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule }
    from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { FileUploadModule } from 'primeng/fileupload';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        HttpClientModule,
        FileUploadModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule { }


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG FileUpload Templates using pTemplate=”content”.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG FileUpload Templates</h5>
  
<p-fileUpload 
    name="geeks[]" url="./upload.php" 
    multiple="multiple" accept=".txt" 
    maxFileSize="1000000">
    <ng-template pTemplate="content">
        <ul *ngIf="uploadedToGfG.length">
            <li *ngFor="let gfg of uploadedToGfG">
                {{gfg.name}} - {{gfg.size}} bytes
            </li>
      </ul>
    </ng-template>
</p-fileUpload>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    uploadedToGfG: any[] = [];
  
    onUpload(event) {
        for (let item of event.items) {
            this.uploadedToGfG.push(item);
        }
    }
}


  • app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule }
    from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { FileUploadModule } from 'primeng/fileupload';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        HttpClientModule,
        FileUploadModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule { }


Output:

 

Reference: https://primefaces.org/primeng/fileupload



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads