Why nzBeforeUpload with option multiple is called multiple times

Viewed 796

I'm using Angular 10 and ng-zorro and I am using nz-upload component, and I have a question:

Why beforeUpload is called equal to files uploaded? if we have a nzFileUpload[] parameter, its like a repetition not wanted

My .html code:

 <div>
   <nz-upload
      ngDefaultControl
      nzType="drag"
      (click)="form.get('files').markAsTouched()"
      [nzDirectory]="false"
      [nzBeforeUpload]="beforeUpload"
      [nzDisabled]="false"
      [nzLimit]="fileMaxQuantity"
      [nzSize]="0"
      [nzListType]="'text'"
      [nzMultiple]="true"
      [nzShowUploadList]="false"
      [nzShowButton]="false"
      (nzChange)="handleChange($event)"
       >
       <p class="ant-upload-drag-icon" style="margin-bottom: unset;">
          <i nz-icon nzType="cloud-upload"></i>
        </p>
        <p class="ant-upload-text">{{ '::FileUpload:FilesUploadMessage' | abpLocalization }}
        </p>
        <p class="ant-upload-hint">
           {{ '::FileUpload:FilesUploadHint' | abpLocalization }}
        </p>
    </nz-upload>
    <mat-error *ngIf="form.get('files').hasError('required') && form.get('files').touched">
       {{ '::FileUpload:FilesFieldRequired' | abpLocalization}}
     </mat-error>
    </div>

And my .ts (beforeupload):

beforeUpload = (singleFile: File, fileList: File[]): boolean => {
    // if (this.form.controls.files as FormArray)
    const fileNames = this.form.controls.fileNames.value as [];
    if (fileNames.length === this.fileMaxQuantity) {
      this.snackBarService.warning(this.localizationService.instant('::FileUpload:NumberFilesExceedsAllowed'), true);
      return false;
    } else {
      for (let i = 0; i <= fileList.length; i++) {
        const file = fileList[i];
      // _.each(fileList, (file) => {
        if (this.form.controls.fileNames.value.length === this.fileMaxQuantity) {
          this.snackBarService.warning(this.localizationService.instant('::FileUpload:NumberFilesExceedsAllowed'));
          break;
          // return false;
        } else {
          const tempStackSize = this.actualFileStackSize + file.size;
          if (file.size > this.fileMaxSize || tempStackSize > this.fileMaxSize) {
            this.snackBarService.warning(this.localizationService.instant('::FileUpload:FileTooHeavy'), true);
            return false;
          } else if ( !this.fileList.some( p => p.name === file.name ) ) {
            const ext = this.extensionPipe.transform(file.name);
            let control: FormControl;
            (this.form.controls.files as FormArray).push(new FormControl(file.name));
            (!this.regexWithExt.test(file.name)) ?
              this.fileListRequired.push(true) : // debe cambiar filename
              this.fileListRequired.push(false);

            control = new FormControl(file.name.replace(ext, ''),
              [ Validators.pattern(this.regex),
                Validators.required,
                Validators.maxLength(this.fileNameMaxLength - ext.length),
                this.fileNameValidator()]);
            control.markAllAsTouched();
            (this.form.controls.fileNames as FormArray).push(control);
            // fileNames.push(file.name);
            this.fileList.push(file);
            this.actualFileStackSize = tempStackSize;
          }
        }
      }
      // fileList.forEach((file) => {
      // });
    }
    return false;
  }

From the above, the problem is that this function beforeUpload is called equal to list size of files.

0 Answers
Related