Open In App

Angular PrimeNG FileUpload Templating

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 Templating 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:

 



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 Templating using content Templating.




<h1 style="color: green">GeeksforGeeks</h1>
<h4> Angular PrimeNG FileUpload Templating.</h4>
  
<p-fileUpload name="demo[]"
    url="./upload.php"
    (onUpload)="onUpload($event)"
    multiple="multiple" accept=".txt"
    maxFileSize="1000000">
    <ng-template pTemplate="content">
        <ul *ngIf="uploadedGFG.length">
            <li *ngFor="let gfg of uploadedGFG">
            {{gfg.name}} - {{gfg.size}} bytes
        </li>
        </ul>
    </ng-template>
</p-fileUpload>




import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [MessageService],
})
  
export class AppComponent {
    uploadedGFG: any[] = [];
  
    constructor(private messageService: MessageService,
        private primengConfig: PrimeNGConfig
    ){}
  
    onUpload(event) {
        for (let file of event.files) {
        this.uploadedGFG.push(file);
        }
  
        this.messageService.add({
            severity: 'info',
            summary: 'File Uploaded',
            detail: 'File uploaded successfully',
        });
    }
}




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 Templating using both toolbar and content Templating.




<h1 style="color: green">GeeksforGeeks</h1>
<h4>Angular PrimeNG FileUpload Templating.</h4>
  
<p-fileUpload name="demo[]"
    url="./upload.php"
    (onUpload)="onUpload($event)" 
    accept=".txt"
    maxFileSize="1000000">
    <ng-template pTemplate="toolbar">
        <small style="color: red">
              Please upload any file.
          </small>
    </ng-template>
    <ng-template pTemplate="content">
        <ul *ngIf="uploadedGFG.length">
          <li *ngFor="let gfg of uploadedGFG">
            {{gfg.name}} - {{gfg.size}} bytes
          </li>
        </ul>
    </ng-template>
</p-fileUpload>




import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [MessageService],
})
  
export class AppComponent {
    uploadedGFG: any[] = [];
  
    constructor(private messageService: MessageService,
        private primengConfig: PrimeNGConfig
    ){}
  
    onUpload(event) {
        for (let file of event.files) {
        this.uploadedGFG.push(file);
        }
  
        this.messageService.add({
            severity: 'info',
            summary: 'File Uploaded',
            detail: 'File uploaded successfully',
        });
    }
}




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


Article Tags :