File

core/file-picker-form-control/file-picker-form-control.component.ts

Description

File uploader

Implements

OnInit AfterViewInit ControlValueAccessor Validator

Metadata

Index

Properties
Methods
Inputs
Outputs
HostListeners

Constructor

constructor(filesService: FilesService, injector: Injector)
Parameters :
Name Type Optional
filesService FilesService No
injector Injector No

Inputs

accept
Type : string

Specifies a filter for what file types the user can pick from the file input dialog box. By default all types are accepted.

Specify file types by extensions:

Example :
 ...
 [accept]="'.zip,.7z'"
 ...

Specify file types by extensions and generic types GENERIC_FILE_TYPE:

Example :
 ...
 [accept]="'.pdf,archive'"
 ...

Specify file types by generic types GENERIC_FILE_TYPE:

Example :
...
[accept]="'archive,video'"
...
disableValidators
Type : IAvailableValidators
Default value : {}

Allows to disable default validators. Accepts object with following optional properties:

  • fileMaxSize - Providing true disables default validation for file size, accepts only boolean
  • fileNonEmpty - Providing true disables default validation for empty files, accepts only boolean
  • fileNameMaxLength - Providing true disables default validation for file name length, accepts only boolean
maxAllowedFiles
Type : number
Default value : Infinity

Sets how many files can be submitted, accepts only numbers.

minRequiredFiles
Type : number
Default value : 1

Sets the minimum number of files to be uploaded, accepts only numbers.

uploadInProgress
Type : boolean
Default value : false

Displays upload progress bar, accepts only boolean.

uploadProgress
Type : IFetchWithProgress[]

The array of progress state objects for each selected file (matched by index).

validateExtensionOnDrag
Type : boolean
Default value : false

Display warning if any file dragged over the drop area has unsupported extension.

Outputs

dropped
Type : EventEmitter<File[]>

Emits an event with the array of File objects representing the dropped files.

HostListeners

window:dragover
Arguments : '$event'
window:dragover(e)
window:drop
Arguments : '$event'
window:drop(e)

Methods

afterValueChanged
afterValueChanged()
Returns : void
deleteAt
deleteAt(index: number)
Parameters :
Name Type Optional
index number No
Returns : void
filesSelected
filesSelected(ev: Event)
Parameters :
Name Type Optional
ev Event No
Returns : void
ngAfterViewInit
ngAfterViewInit()
Returns : void
ngOnInit
ngOnInit()
Returns : void
onDragOver
onDragOver(e)
Decorators :
@HostListener('window:dragover', ['$event'])
Parameters :
Name Optional
e No
Returns : void
onDrop
onDrop(e: DragEvent)
Parameters :
Name Type Optional
e DragEvent No
Returns : void
onWindowDrop
onWindowDrop(e)
Decorators :
@HostListener('window:drop', ['$event'])
Parameters :
Name Optional
e No
Returns : void
registerOnChange
registerOnChange(fn: never)
Parameters :
Name Type Optional
fn never No
Returns : void
registerOnTouched
registerOnTouched(fn: never)
Parameters :
Name Type Optional
fn never No
Returns : void
registerOnValidatorChange
registerOnValidatorChange(fn: () => void)
Parameters :
Name Type Optional
fn function No
Returns : void
setDisabledState
setDisabledState(isDisabled: boolean)
Parameters :
Name Type Optional
isDisabled boolean No
Returns : void
validate
validate(control: AbstractControl)
Parameters :
Name Type Optional
control AbstractControl No
Returns : ValidationErrors | null
writeValue
writeValue(files: File[] | null)
Parameters :
Name Type Optional
files File[] | null No
Returns : void

Properties

acceptedExts
Type : string[]
controlInstance
Type : AbstractControl
disabled
Type : boolean
disabledValidators
Type : IAvailableValidators
Default value : { fileMaxSize: false, fileNonEmpty: false, fileNameMaxLength: false }
droppedFiles
Type : File[]
Default value : []
errors
Type : ValidationErrors
filePlaceholder
Type : ElementRef<HTMLElement>
Decorators :
@ViewChild('filePlaceholder', {static: true})
isDraggedFileUnsupported$
Type : Observable<boolean>
isDraggingFiles$
Type : Observable<boolean>
isDraggingOverFilePlaceholder$
Type : Observable<boolean>
isDropAreaEnlarged$
Type : Observable<boolean>
isExtensionAllowed$
Type : Observable<boolean>
loadingMessage
Default value : gettext('Upload in progress')
valid
Type : boolean

File picker form control

Allows to select a binary for upload. Component provides drop area functionality allowing you to just drop files within specified area. Attributes:

  • maxAllowedFiles maximum number of total files to be selected
  • minRequiredFiles minimum number of total files to be selected
  • accept type of files allowed for upload
  • disableValidators disable specific validators
  • dropped function callback to be invoked upon drop action
  • uploadInProgress boolean flag indicating whenever there is upload ongoing
  • uploadProgress the array of progress state objects for each selected file
  • validateExtensionOnDrag turn on/off validation on file drag event

Usage Example:

Example :
<div>
    <c8y-file-picker-form-control
        #filePicker
        [formControl]="control"
        [uploadInProgress]="isLoading"
        (dropped)="onFileDroppedEvent($event)"
        [uploadProgress]="uploadProgress"
    ></c8y-file-picker-form-control>
</div>

<c8y-form-group class="m-0">
  <div
    class="file-picker-drop-zone"
    [class.dragging]="isDropAreaEnlarged$ | async"
    tabindex="0"
  >
    <div
      class="file-placeholder pointer"
      [class.drag-over]="isDraggingOverFilePlaceholder$ | async"
      #filePlaceholder
      (drop)="onDrop($event)"
      (click)="!uploadInProgress && picker.click()"
    >
      <div *ngIf="validateExtensionOnDrag && (isDraggedFileUnsupported$ | async); else defaultHint">
        <p class="form-control-static text-truncate">
          <i
            class="text-warning m-r-4"
            c8yIcon="exclamation-triangle"
          ></i>
          <span>{{ 'At least one file is of an unsupported type.' | translate }}</span>
        </p>
      </div>
      <ng-template #defaultHint>
        <div
          class="hint-placeholder"
          *ngIf="!uploadInProgress"
        >
          <i c8yIcon="upload"></i>
          <p>
            <b>{{ 'Drop files here or click to browse' | translate }}</b>
          </p>
          <p
            class="m-t-8 text-12 text-muted"
            *ngIf="accept"
          >
            {{ 'Supported file types:' | translate }} {{ accept }}
          </p>
          <c8y-form-group>
            <c8y-messages
              class="has-error"
              *ngIf="
                controlInstance.touched && controlInstance.errors && controlInstance.errors
                  | filterNonArrayValidationErrors
              "
            >
              <c8y-message
                *ngFor="
                  let error of controlInstance.errors | filterNonArrayValidationErrors | keyvalue
                "
              >
                {{ error.key | humanizeValidationMessage | translate: error.value }}
              </c8y-message>
            </c8y-messages>
          </c8y-form-group>
        </div>
        <div
          class="d-flex d-col p-4 flex-center"
          *ngIf="uploadInProgress"
        >
          <c8y-loading></c8y-loading>
          <p class="m-t-auto m-b-auto m-r-8 text-center">
            {{ loadingMessage | translate }}
          </p>
        </div>
      </ng-template>
    </div>
  </div>
  <div class="file-container">
    <ul class="list-group">
      <ng-container *ngFor="let file of droppedFiles; let i = index">
        <li class="list-group-item p-4 a-i-center">
          <div class="d-flex a-i-center">
            <i
              class="icon-20 m-r-8"
              [c8yIcon]="file | fileIcon"
            ></i>
            <div
              class="m-r-16 text-truncate"
              [title]="file.name"
            >
              {{ file.name }}
            </div>
            <div class="m-r-16 text-nowrap">({{ file.size | bytes }})</div>
            <div class="m-l-auto">
              <button
                class="btn btn-dot btn-dot--danger"
                title="{{ 'Remove' | translate }}"
                [attr.aria-label]="'Remove' | translate"
                type="button"
                *ngIf="!uploadInProgress"
                (click)="deleteAt(i)"
              >
                <i c8yIcon="minus-circle"></i>
              </button>
            </div>
          </div>
          <c8y-form-group>
            <c8y-messages class="has-error">
              <c8y-message
                *ngFor="
                  let error of controlInstance.errors | extractArrayValidationErrors: i | keyvalue
                "
              >
                {{ error.key | humanizeValidationMessage | translate: error.value }}
              </c8y-message>
            </c8y-messages>
          </c8y-form-group>

          <div
            class="d-block"
            data-cy="c8y-file-picker-form-control--upload-progress-bar"
            *ngIf="uploadInProgress"
          >
            <div
              class="progress progress-striped active"
              *ngIf="uploadProgress"
            >
              <div
                class="progress-bar progress-bar-info"
                [style.width]="uploadProgress[i].percentage + '%'"
                aria-valuenow="0"
                aria-valuemin="0"
                aria-valuemax="100"
                role="progressbar"
              >
                {{ uploadProgress[i].percentage }}%
              </div>
            </div>
          </div>
        </li>
      </ng-container>
    </ul>
  </div>
  <input
    class="hidden"
    id="file"
    type="file"
    #picker
    (change)="filesSelected($event)"
    [accept]="acceptedExts"
    [multiple]="maxAllowedFiles > 1"
  />
</c8y-form-group>

results matching ""

    No results matching ""