How to check file size before upload in Angular Uploader
31 Aug 20262 minutes to read
Use the uploading event to obtain file sizes before uploading to the server. The args.fileData object reports size in bytes; convert it to KB or MB with the bytesToSize method on the UploaderComponent instance.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { UploaderModule } from '@syncfusion/ej2-angular-inputs'
import { Component, ViewChild } from '@angular/core';
import { EmitType } from '@syncfusion/ej2-base';
import { UploaderComponent } from '@syncfusion/ej2-angular-inputs';
@Component({
imports: [
UploaderModule
],
standalone: true,
selector: 'app-root',
templateUrl: './default.html',
styleUrls: ['./index.css']
})
export class AppComponent {
@ViewChild('defaultupload')
public uploadObj?: UploaderComponent;
public autoUpload: boolean = false;
public path: Object = {
saveUrl: 'https://services.syncfusion.com/angular/production/api/FileUploader/Save',
removeUrl: 'https://services.syncfusion.com/angular/production/api/FileUploader/Remove'
};
public onBeforeUpload: EmitType<Object> = (args: any) => {
// get the file size in bytes
let sizeInBytes: number = args.fileData.size;
// get the file size in standard format
alert("File size is: " + this.uploadObj?.bytesToSize(sizeInBytes));
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));You can also explore the Angular File Upload feature tour page for its groundbreaking features. Explore our Angular File Upload example to understand how to browse and select the files which you want to upload to the server.