Model Binding

The FileSelect allows you to bind its value to the model and render initial files by using the ngModel directive.

Contrary to the Upload component, the value of the FileSelect is a collection of File instances.

The [(ngModel)] value gets updated when files are successfully selected or removed. To access all selected files and their details, you could use the event data in the select and remove event handlers. The event.files field provides the array of file details.

Binding of File Collections

The FileSelect emits values of type File[] each time a file is selected or removed.

import { Component, ViewEncapsulation } from '@angular/core';
import { FileInfo } from '@progress/kendo-angular-upload';

@Component({
    selector: 'my-app',
    styles: [`
      .example-config {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
      }

      .list-title {
        align-self: center;
        margin-top: 5px;
        margin-bottom: 5px;
      }
    `],
    template: `
      <div class = "example-config" >
          <div class="text-box" [style.width.%] = "80">
            <div class="list-title">
              {{ myFiles != null ? 'Files currently bound to the model:' : 'No files are currently bound to the model' }}
            </div>
            <div *ngFor='let file of myFiles'>
              {{file.name}}
            </div>
          </div>
          <div class="button-box" [style.height.px]="30">
            <button role="button" class="k-button" (click)="clearModel()">Clear Model</button>
          </div>
      </div>

      <kendo-fileselect [(ngModel)]="myFiles">
      </kendo-fileselect>
    `
})
export class AppComponent {
    public myFiles: Array<File>;

    public clearModel(): void {
        this.myFiles = null;
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UploadsModule } from '@progress/kendo-angular-upload';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

@NgModule({
    imports: [BrowserModule, HttpClientModule, UploadsModule, BrowserAnimationsModule, FormsModule, ReactiveFormsModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})

export class AppModule {}
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

enableProdMode();

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

Rendering of Initial Files

The ngModel directive allows the FileSelect to render initial files in the list. This could be achieved by passing a collection containing the list of files for display. Those could be of type:

  • File - actual files, which were previously uploaded, created client side etc.
  • FileInfo - mocks of previously uploaded files, which only contain information for display. The FileInfo object requires just the name of the file to be specified, but you can also specify its size.

To set the initially rendered files, either:

  • Use a one-way binding with [ngModel]. As a result, the FileSelect will use only the ngModel value to initially fill the files list, or
  • Use a two-way binding with [(ngModel)]. As a result, when the user selects new files, the FileSelect will also update the ngModel value.
import { Component } from '@angular/core';
import { FileInfo } from '@progress/kendo-angular-upload';

@Component({
    selector: 'my-app',
    styles: [`
      .example-config {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
      }

      .list-title {
        align-self: center;
        margin-top: 5px;
        margin-bottom: 5px;
      }
    `],
    template: `
      <div class = "example-config" >
          <div class="text-box" [style.width.%] = "80">
            <div class="list-title">
              {{ myFiles != null ? 'Files currently bound to the model:' : 'No files are currently bound to the model' }}
            </div>
            <div *ngFor='let file of myFiles'>
              {{file.name}}
            </div>
          </div>
          <div class="button-box" [style.height.px]="30">
            <button role="button" class="k-button" (click)="clearModel()">Clear Model</button>
          </div>
      </div>

      <kendo-fileselect [(ngModel)]="myFiles">
      </kendo-fileselect>
    `
})
export class AppComponent {
    public myFiles: Array<any> = [
        { name: 'First.txt', size: 500 },
        { name: 'Second.jpg', size: 100 }
    ];

    public clearModel(): void {
        this.myFiles = null;
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UploadsModule } from '@progress/kendo-angular-upload';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

@NgModule({
    imports: [BrowserModule, HttpClientModule, UploadsModule, BrowserAnimationsModule, FormsModule, ReactiveFormsModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})

export class AppModule {}
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

enableProdMode();

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

In this article