How can I help you?
Check the MIME type of file before uploading it in React Uploader component
21 Feb 20263 minutes to read
Validate file types before upload by checking the MIME type using the uploading event. The following sample demonstrates displaying the file MIME type in an alert box before the upload begins.
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'
};
onBeforeUpload(args) {
// get the file MIME type
alert("File MIME type is: " + args.fileData.rawFile.type);
}
render() {
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'));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: 'https://services.syncfusion.com/react/production/api/FileUploader/Remove',
saveUrl: 'https://services.syncfusion.com/react/production/api/FileUploader/Save'
}
public onBeforeUpload(args: UploadingEventArgs): void {
// get the file MIME type
alert("File MIME type is: " + (args.fileData.rawFile as Blob).type);
}
public render() {
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'));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.