Open In App

Angular PrimeNG FileUpload Custom Upload

Last Updated : 14 Nov, 2022
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 see how to use the FileUpload Custom Upload in Angular PrimeNG.

Angular PrimeNG FileUpload Custom Upload is used to override uploading implementation by defining a custom upload handler event.

Angular PrimeNG FileUpload Custom Upload Properties:

  • name: It is the name of the request parameter to recognize the uploaded file at the backend.
  • url: It is the remote URL to upload the file.
  • customUpload: It is used to define whether to use the default upload or use the custom upload implementation defined using the uploadHandler.
  • auto: It is a boolean property, which when enabled, the file(s) will be uploaded automatically upon selection.

 

Syntax:

// In app.component.html
<p-fileUpload name="..."
                url="..."
              [customUpload]="true"
              (uploadHandler)="function()">
</p-fileUpload>

// In app.component.ts
function() {
  // function content
}

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:

 

Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG FileUpload Custom Upload.

  • app.component.html

HTML




<h2 style="color: green">
  GeeksforGeeks
</h2>
<h4>
      Angular PrimeNG FileUpload Custom Upload
</h4>
  
<p-fileUpload name="myfile[]"
              url="./upload.php"
              [customUpload]="true"
              (uploadHandler)="uploadfun($event)"
              [auto]="true">
</p-fileUpload>


  • app.component.ts

Javascript




import { Component, OnInit } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [],
})
export class AppComponent {
    constructor() { }
    ngOnInit() { }
    uploadfun(event) {
        console.log(event);
    }
}


  • 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 demonstrating the use of the Angular PrimeNG FileUpload Custom Upload, where we are uploading multiple files using drag and drop with a custom upload function.

  • app.component.html

HTML




<h2 style="color: green">
  GeeksforGeeks
</h2>
<h4>
      Angular PrimeNG FileUpload Custom Upload
</h4>
  
<p-fileUpload name="myfile[]"
              url="./upload.php"
              multiple="multiple"
              [customUpload]="true"
              (uploadHandler)="uploadfun($event)"
              [auto]="true">
</p-fileUpload>


  • app.component.ts

Javascript




import { Component, OnInit } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [],
})
export class AppComponent {
    constructor() { }
    ngOnInit() { }
    uploadfun(event) {
        console.log("Multiple Files are uploaded: ", event.files);
    }
}


  • 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