primeNG p-fileUpload自定义模式回调未触发

时间:2018-12-05 03:43:03

标签: angular primeng

primeNG当我启用customeMode时,p-fileUpload不会触发上载处理程序。 您能帮我解决这个问题吗?

我需要手动发出服务器请求。

   <p-fileUpload name="invoiceFiles[]" customUpload="true" [showUploadButton]="false" multiple="multiple"
                [showCancelButton]="false" (uploadHandler)="invoiceUpload($event)">
              </p-fileUpload>

1 个答案:

答案 0 :(得分:1)

如果隐藏了上载按钮,则需要另一种捕获所选文件的方法。一种方法是使用onSelect将文件存储在可以与表单一起提交的数组中。这是一个示例:

<p-fileUpload name="demo[]" #fileInput (onSelect)="onSelect($event)" [showUploadButton]="false" customUpload="true"></p-fileUpload>

在控制器中:

import { FormArray, FormControl, FormGroup, FormBuilder, Validators} from '@angular/forms';

private myFormFilesToUpload: FormArray;

constructor(private _fb: FormBuilder) {}

this.myForm = this._fb.group({
        //...other form controls
        filesToUpload: this._fb.array([])
});

this.myFormFilesToUpload = this.myForm.get('filesToUpload') as FormArray;

onSelect(event) {
    if (event.files && event.files.length > 0) {
        this.myForm.markAsDirty();
        this.myFormFilesToUpload.push(this._fb.group({
            fieldName: 'my_images',
            files: this._fb.array(Array.from(event.files))
        }));
    }
}

然后,您提交表格后,文件应发送出去。