Getting Started with KendoReact Upload

The KendoReact Upload helps users send files from their file systems to dedicated server handlers which are configured to receive them.

The KendoReact Upload component is part of the KendoReact library of React UI components. It is distributed through NPM under the kendo-react-upload package.

Basic Usage

The following example demonstrates the Upload in action.

import React from 'react';
import ReactDOM from 'react-dom';
import { Upload } from '@progress/kendo-react-upload';

class App extends React.Component {
    render() {
        return (
            <Upload
                batch={false}
                multiple={true}
                defaultFiles={[]}
                withCredentials={false}
                saveUrl={'https://demos.telerik.com/kendo-ui/service-v4/upload/save'}
                removeUrl={'https://demos.telerik.com/kendo-ui/service-v4/upload/remove'}
            />
        );
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

Functionality and Features

Installation

  1. Download and install the package. Use Node.js v5.0.0 or later.

    npm install --save @progress/kendo-react-upload
  2. Once installed, import the package module.

    // ES2015 module syntax
    import { Upload } from '@progress/kendo-react-upload';
    // CommonJS format
    const { Upload } = require('@progress/kendo-react-upload');
  3. You are required to install one of the Kendo UI themes to style your components. For more information on how to add the styles, refer to the article on getting started.

Dependencies

The Upload package requires the following peer dependencies that have to be installed by your application:

  • react
  • react-dom

Events

The following example demonstrates basic Upload events.

import React from 'react';
import ReactDOM from 'react-dom';
import { Upload } from '@progress/kendo-react-upload';

const fileStatuses = [
    'UploadFailed',
    'Initial',
    'Selected',
    'Uploading',
    'Uploaded',
    'RemoveFailed',
    'Removing'
];

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            files: [],
            events: [],
            filePreviews: {}
        };
    }

    onAdd = (event) => {
        const afterStateChange = () => {
            event.affectedFiles
                .filter(file => !file.validationErrors)
                .forEach(file => {
                    const reader = new FileReader();

                    reader.onloadend = (ev) => {
                        this.setState({
                            filePreviews: {
                                ...this.state.filePreviews,
                                [file.uid]: ev.target.result
                            }
                        });
                    };

                    reader.readAsDataURL(file.getRawFile());
                });
        };

        this.setState({
            files: event.newState,
            events: [
                ...this.state.events,
                `File selected: ${event.affectedFiles[0].name}`
            ]
        }, afterStateChange);
    }

    onRemove = (event) => {
        const filePreviews = {
            ...this.state.filePreviews
        };

        event.affectedFiles.forEach(file => {
            delete filePreviews[file.uid];
        });

        this.setState({
            files: event.newState,
            events: [
                ...this.state.events,
                `File removed: ${event.affectedFiles[0].name}`
            ],
            filePreviews: filePreviews
        });
    }

    onProgress = (event) => {
        this.setState({
            files: event.newState,
            events: [
                ...this.state.events,
                `On Progress: ${event.affectedFiles[0].progress} %`
            ]
        });
    }

    onStatusChange = (event) => {
        const file = event.affectedFiles[0];

        this.setState({
            files: event.newState,
            events: [
                ...this.state.events,
                `File '${file.name}' status changed to: ${fileStatuses[file.status]}`
            ]
        });
    }

    render() {
        return (
            <div>
                <Upload
                    batch={false}
                    multiple={true}
                    files={this.state.files}
                    onAdd={this.onAdd}
                    onRemove={this.onRemove}
                    onProgress={this.onProgress}
                    onStatusChange={this.onStatusChange}
                    withCredentials={false}
                    saveUrl={'https://demos.telerik.com/kendo-ui/service-v4/upload/save'}
                    removeUrl={'https://demos.telerik.com/kendo-ui/service-v4/upload/remove'}
                />
                 <div className={'example-config'} style={{ marginTop: 20 }}>
                    <ul className={'event-log'}>
                        {
                            this.state.events.map(event => <li>{event}</li>)
                        }
                    </ul>
                </div>
                {
                    this.state.files.length ?
                    <div className={'img-preview example-config'}>
                        <h3>Preview selected images</h3>
                        {
                            Object.keys(this.state.filePreviews)
                                .map((fileKey) => (<img
                                    src={this.state.filePreviews[fileKey]}
                                    alt={'image preview'}
                                    style={{ width: 200, margin: 10 }}
                                />))
                        }
                    </div> : undefined
                }
            </div>
        );
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);
 /