Open In App

Angular PrimeNG FileUpload File Types

Last Updated : 08 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. This article will show us how to use FileUpload File Types in Angular PrimeNG.

The FileUpload Component is used to take file input from the user. FileUpload File Types can be used to restrict the type of the uploaded file. It is specified using the accept property.

Angular PrimeNG FileUpload File Size Properties:

  • name: It is the name of the request parameter which will be used to identify the file on the server side.
  • url: It is the remote URL to upload the file.
  • accept: This property is used to define the file type that can be uploaded using the file input.

 

Syntax:

<p-fileUpload accept="type"></p-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:

 

Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG FileUpload File Types, where we are setting the accept property to images and accepting only images.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG FileUpload File Types</h4>
  
<p-fileUpload mode="basic"
              multiple="multiple"
              accept="image/*"
</p-fileUpload>


  • app.component.ts

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent { }


  • 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 { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        FileUploadModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
export class AppModule { }


Output:

 

Example 2: Below is another example demonstrating the use of the Angular PrimeNG FileUpload File Types, where we are accepting pdf files only, so uploading images will give us an error.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG FileUpload File Types</h4>
<p-fileUpload multiple="multiple" 
              accept=".pdf"
</p-fileUpload>


  • app.component.ts

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent { }


  • 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 { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        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