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
Best Landlord Insurance In Pennsylvania For Your Rental Property (Rates from $98/month!) - Simply Insurance
NYS Emergency Rental Assistance Program
Craigslist Myrtle Beach Motorcycles For Sale By Owner
Lengua With A Tilde Crossword
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Instructional Resources
Windcrest Little League Baseball
Dollywood's Smoky Mountain Christmas - Pigeon Forge, TN
Kokichi's Day At The Zoo
Pickswise the Free Sports Handicapping Service 2023
Moviesda Dubbed Tamil Movies
Craigslist Dog Sitter
AB Solutions Portal | Login
World History Kazwire
Newgate Honda
Pvschools Infinite Campus
Socket Exception Dunkin
Discover Westchester's Top Towns — And What Makes Them So Unique
Craigslist Pets Athens Ohio
Peraton Sso
Bahsid Mclean Uncensored Photo
Costco Gas Foster City
Daily Voice Tarrytown
Water Days For Modesto Ca
Trivago Sf
Swgoh Blind Characters
20 Different Cat Sounds and What They Mean
Eine Band wie ein Baum
The BEST Soft and Chewy Sugar Cookie Recipe
Ppm Claims Amynta
Governor Brown Signs Legislation Supporting California Legislative Women's Caucus Priorities
Yonkers Results For Tonight
SN100C, An Australia Trademark of Nihon Superior Co., Ltd.. Application Number: 2480607 :: Trademark Elite Trademarks
The Many Faces of the Craigslist Killer
Dove Cremation Services Topeka Ks
4.231 Rounded To The Nearest Hundred
Play It Again Sports Forsyth Photos
Elijah Streams Videos
Issue Monday, September 23, 2024
Sephora Planet Hollywood
How To Paint Dinos In Ark
The best bagels in NYC, according to a New Yorker
How I Passed the AZ-900 Microsoft Azure Fundamentals Exam
Random Animal Hybrid Generator Wheel
How to Install JDownloader 2 on Your Synology NAS
From Grindr to Scruff: The best dating apps for gay, bi, and queer men in 2024
Verizon Forum Gac Family
Clock Batteries Perhaps Crossword Clue
Escape From Tarkov Supply Plans Therapist Quest Guide
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.