Search results

Check File Size before Uploading it in React Uploader component

By using the uploading event, you can get the file size before uploading it to the server. File object contains the file size in bytes only. You can convert the size to standard formats (KB or MB) using bytesToSize method.

import { UploaderComponent, UploadingEventArgs } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component<{}, {}> {
  public uploadObj: UploaderComponent;
  public path: object = {
      removeUrl: '',
      saveUrl: ''
  public onBeforeUpload(args: UploadingEventArgs): void {
    // get the file size in bytes
    const sizeInBytes: number = args.fileData.size;
    // get the file size in standard format
    alert("File size is: " + this.uploadObj.bytesToSize(sizeInBytes));
  public render(): JSX.Element {
    return (
      <UploaderComponent autoUpload={false} ref = {upload => {this.uploadObj = upload !}} asyncSettings={this.path} uploading={this.onBeforeUpload = this.onBeforeUpload.bind(this)} />

ReactDOM.render(<App />, document.getElementById('fileupload'));