Open In App

Angular PrimeNG FileUpload File Types

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:

 



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.




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




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




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.




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




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




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


Article Tags :