Open In App

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 FileUpload Advanced Mode mode provides a simpler UI as an alternative to basic mode.



Angular PrimeNG FileUpload Advanced Properties:

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

npm start

Example 1: Below is an example code that illustrates the use of Angular PrimeNG FileUpload Advanced Component using single file upload.

app.component.html:




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

app.component.ts:




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',
        });
    }
}

app.module.ts:




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 Advanced Component using multiple file upload.

app.component.html:




<h1 style="color: green">GeeksforGeeks</h1>
<h4> Angular PrimeNG FileUpload Advanced Component.</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 uploadedFiles">
              {{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',
        });
    }
}

app.module.ts:




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 :