Open In App

Angular PrimeNG FileUpload File Size

Last Updated : 07 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 Size in Angular PrimeNG.

The FileUpload Component is used to take file input from the user. FileUpload File Size can be used to restrict the maximum file size of the uploaded file. It is specified using the maxFileSize 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.
  • maxFileSize: This property is used to define the maximum size in bytes of the file that can be uploaded using the file input. 

 

Syntax:

<p-fileUpload maxFileSize="size"></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:

Project Structure

Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG FileUpload File Size. In this example, we are setting maxFileSize to 1B.

  • app.component.html:

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG FileUpload File Size</h4>
  
<p-fileUpload 
    [maxFileSize]="1" 
    mode="basic" 
    chooseLabel="Upload">
</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 { HttpClientModule } from '@angular/common/http';
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,
        HttpClientModule,
        FileUploadModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
export class AppModule { }


Output:

 

Example 2: Below is another example demonstrating the use of the Angular PrimeNG FileUpload File Size. Here the max uploaded file size is set to 1 MB so files less than 1MB in size can be uploaded.

  • app.component.html:

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG FileUpload File Size</h4>
  
<p-fileUpload
    [maxFileSize]="1000000" 
    [multiple]="true">
</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 { HttpClientModule } from '@angular/common/http';
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,
        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