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

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.vue
    • <template>
    • <DxFileUploader
    • <!-- Uncomment the following line to allow a user to upload multiple files -->
    • <!-- :multiple="true" -->
    • upload-mode="useButtons" <!-- or "instantly" -->
    • upload-url="https://mydomain.com/MyUploadService">
    • </DxFileUploader>
    • </template>
    •  
    • <script>
    • import 'devextreme/dist/css/dx.light.css';
    •  
    • import {
    • DxFileUploader
    • } from 'devextreme-vue/file-uploader';
    •  
    • export default {
    • components: {
    • DxFileUploader
    • },
    •  
    • data() {
    •     return {
    •         //...
    •     };
    • }
    •  
    • };
    • </script>
  • HTML form upload

    App.vue
    • <template>
    • <form id="form"
    • method="post"
    • action="https://mydomain.com/MyUploadService"
    • enctype="multipart/form-data">
    • <DxFileUploader
    • <!-- Uncomment the following line to allow a user to upload multiple files -->
    • <!-- :multiple="true" -->
    • <!-- name="files[]" -->
    • upload-mode="useForm">
    • </DxFileUploader>
    • <input type="submit" >
    • </form>
    • </template>
    •  
    • <script>
    • import 'devextreme/dist/css/dx.light.css';
    •  
    • import {
    • DxFileUploader
    • } from 'devextreme-vue/file-uploader';
    •  
    • export default {
    • components: {
    • DxFileUploader
    • },
    •  
    • data() {
    •     return {
    •         //...
    •     };
    • }
    •  
    • };
    • </script>
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.vue
  • <template>
  • <DxFileUploader
  • name="file"
  • :chunk-size="400000"
  • upload-mode="useButtons" <!-- or "instantly" -->
  • upload-url="https://mydomain.com/MyUploadService">
  • </DxFileUploader>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import {
  • DxFileUploader
  • } from 'devextreme-vue/file-uploader';
  •  
  • export default {
  • components: {
  • DxFileUploader
  • },
  •  
  • data() {
  • return {
  • //...
  • };
  • }
  • };
  • </script>

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.vue
  • <template>
  • <DxFileUploader
  • name="file"
  • @value-changed="addIdParameter"
  • upload-mode="instantly" <!-- or "useButtons" -->
  • upload-url="https://mydomain.com/MyUploadService">
  • </DxFileUploader>
  • <DxNumberBox
  • :value="employee.office"
  • @value-changed="addOfficeParameter"
  • />
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import {
  • DxFileUploader
  • } from 'devextreme-vue/file-uploader';
  • import {
  • DxNumberBox
  • } from 'devextreme-vue/number-box';
  •  
  • export default {
  • components: {
  • DxFileUploader,
  • DxNumberBox
  • },
  •  
  • data() {
  • return {
  • //...
  • };
  • },
  •  
  • methods: {
  • 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;
  • }
  • }
  • }
  • };
  • </script>

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.vue
  • <template>
  • <form id="form"
  • method="post"
  • action="https://mydomain.com/MyUploadService"
  • enctype="multipart/form-data">
  • <input type="hidden" name="id" value="employeeId">
  • <DxFileUploader
  • name="file"
  • @valueChanged="addIdParameter"
  • upload-mode="useForm" />
  • <DxButton
  • text="Update profile"
  • :useSubmitBehavior="true" />
  • </form>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxFileUploader from 'devextreme-vue/file-uploader';
  • import DxNumberBox from 'devextreme-vue/number-box';
  • import DxButton from 'devextreme-vue/button';
  •  
  • const employee = { id: 1, name: "John Heart", position: "CEO", office: 614 };
  • const employeeId: any;
  • const employeeOffice = this.employee.office;
  •  
  • export default {
  • components: {
  • DxFileUploader,
  • DxButton,
  • DxNumberBox
  • },
  •  
  • data() {
  • return {
  • employee,
  • employeeId,
  • employeeOffice
  • };
  • },
  •  
  • methods: {
  • addParameters (e) {
  • this.employeeId = this.employee.id;
  • }
  • }
  • };
  • </script>