Validation in React Uploader component (2024)

23 Jan 202424 minutes to read

The uploader component validate the selected files size and extension using the allowedExtensions, minFileSize and maxFileSize properties.
The files can be validated before uploading to the server and can be ignored on uploading. Also, you can validate the files by setting the HTML attributes to the original input element. The validation process occurs on drag-and-drop the files also.

File type

You can allow the specific files alone to upload using the allowedExtensions property. The extension can be represented as collection by comma separators.
The uploader component filters the selected or dropped files to match against the specified file types and processes the upload operation. The validation happens when you specify value to inline attribute to accept the original input element.

[Class-component]

import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";export default class App extends React.Component { // Uploader component path = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }; render() { return (<UploaderComponent asyncSettings={this.path} allowedExtensions='.doc, .docx, .xls, .xlsx'/>); }}ReactDOM.render(<App />, document.getElementById('fileupload'));
import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";export default class App extends React.Component<{}, {}> {// Uploader component public path: object = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }public render(): JSX.Element{ return ( <UploaderComponent asyncSettings = {this.path} allowedExtensions='.doc, .docx, .xls, .xlsx'/> ); }}ReactDOM.render(<App />, document.getElementById('fileupload'));

[Functional-component]

import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";function App() { // Uploader component const path = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }; return (<UploaderComponent asyncSettings={path} allowedExtensions='.doc, .docx, .xls, .xlsx'/>);}ReactDOM.render(<App />, document.getElementById('fileupload'));
import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";function App(){// Uploader component const path: object = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' } return ( <UploaderComponent asyncSettings = {path} allowedExtensions='.doc, .docx, .xls, .xlsx'/> );}ReactDOM.render(<App />, document.getElementById('fileupload'));

File size

The uploader component allows you to validate the files based on its size. The validation helps to restrict uploading large files or empty files to the server.
The size can be represented in bytes. By default, the uploader component allows to upload minimum file size as 0 byte and maximum file size as 28.4 MB using minFileSize and maxFileSize properties.

[Class-component]

import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";export default class App extends React.Component { // Uploader component path = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }; render() { return (<UploaderComponent asyncSettings={this.path} minFileSize={10000} maxFileSize={28400000}/>); }}ReactDOM.render(<App />, document.getElementById('fileupload'));
import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";export default class App extends React.Component<{}, {}> {// Uploader component public path: object = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }public render(): JSX.Element{ return ( <UploaderComponent asyncSettings = {this.path} minFileSize = {10000} maxFileSize= {28400000} /> ) }}ReactDOM.render(<App />, document.getElementById('fileupload'));

[Functional-component]

import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";function App() { // Uploader component const path = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }; return (<UploaderComponent asyncSettings={path} minFileSize={10000} maxFileSize={28400000}/>);}ReactDOM.render(<App />, document.getElementById('fileupload'));
import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";function App() {// Uploader component const path: object = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' } return ( <UploaderComponent asyncSettings = {path} minFileSize = {10000} maxFileSize= {28400000} /> )}ReactDOM.render(<App />, document.getElementById('fileupload'));

Maximum files count

You can restrict the maximum number of files on uploading using the selected event. In the selected event arguments, you can get the currently selected files details using getFilesData(). You can modify the files details and assign the modified file list to eventArgs.modifiedFilesData.

[Class-component]

import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";export default class App extends React.Component { uploadObj; path = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }; onFileSelected(args) { args.filesData.splice(5); const filesData = this.uploadObj.getFilesData(); const allFiles = filesData.concat(args.filesData); if (allFiles.length > 5) { for (const i of allFiles) { if (i.length > 5) { allFiles.shift(); } } args.filesData = allFiles; args.modifiedFilesData = args.filesData; } args.isModified = true; } render() { return (<UploaderComponent asyncSettings={this.path} selected={this.onFileSelected = this.onFileSelected.bind(this)} ref={upload => { this.uploadObj = upload; }}/>); }}ReactDOM.render(<App />, document.getElementById('fileupload'));
import {FileInfo, SelectedEventArgs, UploaderComponent } 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: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' } public onFileSelected(args : SelectedEventArgs) : void { args.filesData.splice(5); const filesData : FileInfo[] = this.uploadObj.getFilesData(); const allFiles : FileInfo[] = filesData.concat(args.filesData); if (allFiles.length > 5) { for (const i of allFiles) { if ((i as any).length > 5) { allFiles.shift(); } } args.filesData = allFiles; args.modifiedFilesData = args.filesData; } args.isModified = true;}public render(): JSX.Element{ return ( <UploaderComponent asyncSettings = {this.path} selected={this.onFileSelected = this.onFileSelected.bind(this)} ref = {upload => {this.uploadObj = upload !}} />); }}ReactDOM.render(<App />, document.getElementById('fileupload'));

[Functional-component]

import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";function App() { let uploadObj; const path = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }; function onFileSelected(args) { args.filesData.splice(5); const filesData = this.uploadObj.getFilesData(); const allFiles = filesData.concat(args.filesData); if (allFiles.length > 5) { for (const i of allFiles) { if (i.length > 5) { allFiles.shift(); } } args.filesData = allFiles; args.modifiedFilesData = args.filesData; } args.isModified = true; } return (<UploaderComponent asyncSettings={path} selected={this.onFileSelected = onFileSelected.bind(this)} ref={upload => { this.uploadObj = upload; }}/>);}ReactDOM.render(<App />, document.getElementById('fileupload'));
import {FileInfo, SelectedEventArgs, UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";function App() { let uploadObj: UploaderComponent; const path: object = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' } function onFileSelected(args : SelectedEventArgs) : void { args.filesData.splice(5); const filesData : FileInfo[] = uploadObj.getFilesData(); const allFiles : FileInfo[] = filesData.concat(args.filesData); if (allFiles.length > 5) { for (const i of allFiles) { if ((i as any).length > 5) { allFiles.shift(); } } args.filesData = allFiles; args.modifiedFilesData = args.filesData; } args.isModified = true;} return ( <UploaderComponent asyncSettings = {path} selected={onFileSelected = onFileSelected.bind(this)} ref = {upload => {uploadObj = upload !}} />);}ReactDOM.render(<App />, document.getElementById('fileupload'));

Duplicate files

You can validate the duplicate files before uploading to server using the selected event. Compare the selected files with the existing files data and filter the file list by removing the duplicate files.

[Class-component]

import { isNullOrUndefined } from '@syncfusion/ej2-base';import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";export default class App extends React.Component { uploadObj; path = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }; onFileSelected(args) { let existingFiles = this.uploadObj.getFilesData(); for (let i = 0; i < args.filesData.length; i++) { for (const j of existingFiles) { if (!isNullOrUndefined(args.filesData[i])) { if (j.name === args.filesData[i].name) { args.filesData.splice(i, 1); } } } } existingFiles = existingFiles.concat(args.filesData); args.modifiedFilesData = existingFiles; args.isModified = true; } render() { return (<UploaderComponent asyncSettings={this.path} selected={this.onFileSelected = this.onFileSelected.bind(this)} ref={upload => { this.uploadObj = upload; }}/>); }}ReactDOM.render(<App />, document.getElementById('fileupload'));
import { isNullOrUndefined } from '@syncfusion/ej2-base';import {FileInfo, SelectedEventArgs, UploaderComponent } 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: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' } public onFileSelected(args : SelectedEventArgs) : void { let existingFiles: FileInfo[] = this.uploadObj.getFilesData(); for (let i: number = 0; i < args.filesData.length; i++) { for(const j of existingFiles) { if (!isNullOrUndefined(args.filesData[i])) { if (j.name === args.filesData[i].name) { args.filesData.splice(i, 1); } } } } existingFiles = existingFiles.concat(args.filesData); args.modifiedFilesData = existingFiles; args.isModified = true; }public render(): JSX.Element{ return ( <UploaderComponent asyncSettings = {this.path} selected={this.onFileSelected = this.onFileSelected.bind(this)} ref = {upload => {this.uploadObj = upload !}} />); }}ReactDOM.render(<App />, document.getElementById('fileupload'));

[Functional-component]

import { isNullOrUndefined } from '@syncfusion/ej2-base';import { UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";function App() { let uploadObj; const path = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' }; function onFileSelected(args) { let existingFiles = this.uploadObj.getFilesData(); for (let i = 0; i < args.filesData.length; i++) { for (const j of existingFiles) { if (!isNullOrUndefined(args.filesData[i])) { if (j.name === args.filesData[i].name) { args.filesData.splice(i, 1); } } } } existingFiles = existingFiles.concat(args.filesData); args.modifiedFilesData = existingFiles; args.isModified = true; } return (<UploaderComponent asyncSettings={path} selected={onFileSelected = onFileSelected.bind(this)} ref={upload => { uploadObj = upload; }}/>);}ReactDOM.render(<App />, document.getElementById('fileupload'));
import { isNullOrUndefined } from '@syncfusion/ej2-base';import {FileInfo, SelectedEventArgs, UploaderComponent } from '@syncfusion/ej2-react-inputs';import * as React from 'react';import * as ReactDOM from "react-dom";function App() { let uploadObj: UploaderComponent; const path: object = { removeUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove', saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save' } function onFileSelected(args : SelectedEventArgs) : void { let existingFiles: FileInfo[] = this.uploadObj.getFilesData(); for (let i: number = 0; i < args.filesData.length; i++) { for(const j of existingFiles) { if (!isNullOrUndefined(args.filesData[i])) { if (j.name === args.filesData[i].name) { args.filesData.splice(i, 1); } } } } existingFiles = existingFiles.concat(args.filesData); args.modifiedFilesData = existingFiles; args.isModified = true; } return ( <UploaderComponent asyncSettings = {path} selected={onFileSelected = onFileSelected.bind(this)} ref = {upload => {uploadObj = upload !}} />);}ReactDOM.render(<App />, document.getElementById('fileupload'));

You can also explore React File Upload feature tour page for its groundbreaking features. You can also explore our React File Upload example to understand how to browse the files which you want to upload to the server.

See Also

  • Validate image/* on drop
  • Determine whether uploader has file input (required validation)
  • Check file size before uploading it
  • Check the MIME type of file before uploading it
Validation in React Uploader component (2024)
Top Articles
The Future of Wealth Management: How Financial Technology is Revolutionizing the Industry
Religion and Money - How Are They Connected?
Omega Pizza-Roast Beef -Seafood Middleton Menu
Will Byers X Male Reader
Craigslist Warren Michigan Free Stuff
Using GPT for translation: How to get the best outcomes
Kathleen Hixson Leaked
Best Team In 2K23 Myteam
Blackstone Launchpad Ucf
Craigslist - Pets for Sale or Adoption in Zeeland, MI
Cosentyx® 75 mg Injektionslösung in einer Fertigspritze - PatientenInfo-Service
Infinite Campus Parent Portal Hall County
Maxpreps Field Hockey
Weekly Math Review Q4 3
Cvs Learnet Modules
Craigslist Boats For Sale Seattle
Directions To O'reilly's Near Me
Fear And Hunger 2 Irrational Obelisk
Michael Shaara Books In Order - Books In Order
111 Cubic Inch To Cc
Vrachtwagens in Nederland kopen - gebruikt en nieuw - TrucksNL
Mccain Agportal
Rural King Credit Card Minimum Credit Score
Amortization Calculator
Craigslist Personals Jonesboro
Xsensual Portland
All Obituaries | Gateway-Forest Lawn Funeral Home | Lake City FL funeral home and cremation Lake City FL funeral home and cremation
Egusd Lunch Menu
Danielle Moodie-Mills Net Worth
Umn Biology
Restored Republic
Puffin Asmr Leak
WOODSTOCK CELEBRATES 50 YEARS WITH COMPREHENSIVE 38-CD DELUXE BOXED SET | Rhino
King Soopers Cashiers Check
Pixel Combat Unblocked
Rubmaps H
Ff14 Laws Order
The Best Carry-On Suitcases 2024, Tested and Reviewed by Travel Editors | SmarterTravel
Cruise Ships Archives
Weather Underground Corvallis
Brandon Spikes Career Earnings
Anderson Tribute Center Hood River
Mbfs Com Login
Charli D'amelio Bj
Citizens Bank Park - Clio
Top 1,000 Girl Names for Your Baby Girl in 2024 | Pampers
Graduation Requirements
Bellelement.com Review: Real Store or A Scam? Read This
Santa Ana Immigration Court Webex
Electric Toothbrush Feature Crossword
Rétrospective 2023 : une année culturelle de renaissances et de mutations
Lux Nails & Spa
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5953

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.