React FileUploader - Client-Side Settings

Upload Mode

Depending on the uploadMode, the FileUploader UI component uses an HTML form or a FormData interface with a series of Ajax requests to upload files.

View Demo View on GitHub

The following examples show how to configure the FileUploader for each upload mode. Therein, the name property is required to access uploaded files on the server.

  • Ajax upload

    App.js
    • import React from 'react';
    •  
    • import 'devextreme/dist/css/dx.light.css';
    •  
    • import FileUploader from 'devextreme-react/file-uploader';
    •  
    • class App extends React.Component {
    • render() {
    • return (
    • <FileUploader
    • {/* Uncomment the following line to allow a user to upload multiple files */}
    • {/* multiple={true} */}
    • uploadMode="useButtons" {/* or "instantly" */}
    • uploadUrl="https://mydomain.com/MyUploadService">
    • </FileUploader>
    • );
    • }
    • }
    • export default App;
  • HTML form upload

    App.js
    • import React from 'react';
    •  
    • import 'devextreme/dist/css/dx.light.css';
    •  
    • import FileUploader from 'devextreme-react/file-uploader';
    •  
    • class App extends React.Component {
    • render() {
    • return (
    • <form id="form"
    • method="post"
    • action="https://mydomain.com/MyUploadService"
    • enctype="multipart/form-data">
    • <FileUploader
    • {/* Uncomment the following line to allow a user to upload multiple files */}
    • {/* multiple={true} */}
    • {/* name="files[]" */}
    • uploadMode="useForm">
    • </FileUploader>
    • <input type="submit"/>
    • </form>
    • );
    • }
    • }
    • export default App;
NOTE
If you allow a user to upload multiple files using an HTML form, the name property's value must end with square brackets as shown in the commented-out code line in the example above.
See Also

Chunk Upload

Chunk upload allows large files to be divided into parts called "chunks" and sent via multiple requests. To enable this feature, specify the chunk size in bytes and set uploadMode to "instantly" or "useButtons" to send files via Ajax requests.

View Demo

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import FileUploader from 'devextreme-react/file-uploader';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <FileUploader
  • name="file"
  • chunkSize={400000}
  • uploadMode="useButtons" {/* or "instantly" */}
  • uploadUrl="https://mydomain.com/MyUploadService">
  • </FileUploader>
  • );
  • }
  • }
  • export default App;

On the server, you should process the received chunks and merge them into a file. See the Server-Side Implementation examples in ASP.NET and PHP.

Additional Parameters in a Request

If the uploadMode is "instantly" or "useButtons", you can add parameters to the URL by modifying the uploadUrl property. For example, the following code adds an employee ID and an office number:

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import FileUploader from 'devextreme-react/file-uploader';
  • import NumberBox from 'devextreme-react/number-box';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <FileUploader
  • name="file"
  • onValueChanged={this.addIdParameter}
  • uploadMode="instantly" {/* or "useButtons" */}
  • uploadUrl="https://mydomain.com/MyUploadService">
  • </FileUploader>
  • <DxNumberBox
  • value={employee.office}
  • onValueChanged="this.addOfficeParameter">
  • </DxNumberBox>
  • );
  • }
  •  
  • addIdParameter (e) {
  • this.uploadUrl = this.updateQueryStringParameter(this.uploadUrl, "id", this.employee.id);
  • e.component.option("uploadUrl", this.uploadUrl);
  • }
  • addOfficeParameter (e) {
  • if ( e.value !== e.previousValue ) {
  • this.uploadUrl = this.updateQueryStringParameter(this.uploadUrl, "office", e.value);
  • }
  • }
  • updateQueryStringParameter (uri, key, value) {
  • var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  • var separator = uri.indexOf('?') !== -1 ? "&" : "?";
  • if (uri.match(re)) {
  • return uri.replace(re, '$1' + key + "=" + value + '$2');
  • }
  • else {
  • return uri + separator + key + "=" + value;
  • }
  • }
  • }
  • export default App;

When the uploadMode is "useForm", define the parameters within hidden inputs. They are sent to the server in an HTML form along with the files. Some DevExtreme UI components have underlying hidden inputs too. Use the UI component's name property to specify the input's name attribute.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import FileUploader from 'devextreme-react/file-uploader';
  • import NumberBox from 'devextreme-react/number-box';
  • import Button from 'devextreme-react/button';
  •  
  • const App = () => {
  • const addIdParameter = (e) => {
  • employeeId = employee.id;
  • };
  •  
  • const employee = { id: 1, name: "John Heart", position: "CEO", office: 614 };
  • const employeeId: any;
  • const employeeOffice = employee.office;
  •  
  • return (
  • <form id="form"
  • method="post"
  • action="https://mydomain.com/MyUploadService"
  • enctype="multipart/form-data">
  • <FileUploader
  • name="file"
  • onValueChanged={addIdParameter}
  • uploadMode="useForm">
  • </FileUploader>
  • <Button
  • text="Update profile"
  • useSubmitBehavior={true}>
  • </Button>
  • <input type="hidden" name="id" value="employeeId" />
  • </form>
  • );
  • };
  • export default App;