Upload Mode
Depending on the uploadMode, the FileUploader widget uses an HTML form or a FormData interface with a series of Ajax requests to upload files.
The following examples show how to configure the FileUploader for each upload mode. Therein, the name option is required to access uploaded files on the server.
Ajax upload
HTMLTypeScript- <dx-file-uploader
- name="file"
- <!-- Uncomment the following line to allow a user to upload multiple files -->
- <!-- [multiple]="true" -->
- uploadMode="useButtons" <!-- or "instantly" -->
- uploadUrl="https://mydomain.com/MyUploadService">
- </dx-file-uploader>
- import { DxFileUploaderModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- // ...
- }
- @NgModule({
- imports: [
- // ...
- DxFileUploaderModule
- ],
- // ...
- })
HTML form upload
HTMLTypeScript- <form action="https://mydomain.com/MyUploadService" method="post" enctype="multipart/form-data">
- <dx-file-uploader
- name="file"
- <!-- Uncomment the following lines to allow a user to upload multiple files -->
- <!-- [multiple]="true" -->
- <!-- name="files[]" -->
- uploadMode="useForm">
- </dx-file-uploader>
- <input type="submit">
- </form>
- import { DxFileUploaderModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- // ...
- }
- @NgModule({
- imports: [
- // ...
- DxFileUploaderModule
- ],
- // ...
- })
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.
- <dx-file-uploader
- name="file"
- uploadMode="useButtons" <!-- or "instantly" -->
- uploadUrl="https://mydomain.com/MyUploadService"
- [chunkSize]="400000"> <!-- 400 KB -->
- </dx-file-uploader>
- import { DxFileUploaderModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- // ...
- }
- @NgModule({
- imports: [
- // ...
- DxFileUploaderModule
- ],
- // ...
- })
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 option. For example, the following code adds an employee ID and an office number:
- <dx-file-uploader
- name="file"
- (onValueChanged)="addIdParameter($event)"
- [uploadUrl]="uploadUrl"
- uploadMode="instantly"> <!-- or "useButtons" -->
- </dx-file-uploader>
- <dx-number-box
- [(value)]="employee.office"
- (onValueChanged)="addOfficeParameter($event)">
- </dx-number-box>
- import { DxFileUploaderModule, DxNumberBoxModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- employee = { id: 1, name: "John Heart", position: "CEO", office: 614 };
- uploadUrl = "https://mydomain.com/MyUploadService";
- 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;
- }
- }
- }
- @NgModule({
- imports: [
- // ...
- DxFileUploaderModule,
- DxNumberBoxModule
- ],
- // ...
- })
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 widgets have underlying hidden inputs too. Use the widget's name option to specify the input's name
attribute.
- <form action="https://mydomain.com/MyUploadService" method="post" enctype="multipart/form-data">
- <input type="hidden" name="id" [value]="employeeId">
- <dx-file-uploader
- name="file"
- uploadMode="useForm"
- (onValueChanged)="addParameters()">
- </dx-file-uploader>
- <dx-number-box
- [(value)]="employeeOffice"
- name="office">
- </dx-number-box>
- <dx-button
- text="Update profile"
- [useSubmitBehavior]="true">
- </dx-button>
- </form>
- import { DxFileUploaderModule, DxButtonModule, DxNumberBoxModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- employee = { id: 1, name: "John Heart", position: "CEO", office: 614 };
- employeeId: any;
- employeeOffice = this.employee.office;
- addParameters () {
- this.employeeId = this.employee.id;
- }
- }
- @NgModule({
- imports: [
- // ...
- DxFileUploaderModule,
- DxButtonModule,
- DxNumberBoxModule
- ],
- // ...
- })
If you have technical questions, please create a support ticket in the DevExpress Support Center.