angular - PrimeNG - 通过单击按钮发送表单数据和 FileUpload 数据

标签 angular typescript file-upload primeng

在我们的 Angular 项目中,我们有一个包含表单字段和 PrimeNG FileUpload 的表单,我们尝试发送包含所选文件的表单数据。

我们查看了documentation以及网络上的许多示例,但没有一个满足我们的要求(使用“保存”按钮而不是自动上传或“文件上传”按钮发送表单和文件)。

我们尝试了以下方法,将每个模型属性附加到文件上传请求中的文件,但我认为必须有更智能的方法,即在 Save 方法(在 .ts 文件中)中将文件附加到模型属性。

HTML:

<p-fileUpload name="files" url="/MyController/Save" 
    (onBeforeSend)="onBeforeSendFile($event)" 
    (onUpload)="onUploadFile($event)" 
    (onError)="onErrorFile($event)" 
    (onBeforeUpload)="onBeforeUploadFoto($event)" 
    multiple="multiple" 
    chooseLabel="Select" 
    uploadLabel="Load" 
    cancelLabel="Cancel">
</p-fileUpload>

.ts:

//code omitted fo brevity

//at here we append model properties to the array in file upload request:
onBeforeUploadFoto(event: any) {
    event.formData.append('id', this.entityId);
    event.formData.append('name', this.entityName);
}

最佳答案

您可以使用 onSelect 事件:

<p-fileUpload name="files" (onSelect)="dealWithFiles($event)"></p-fileUpload>

在你的组件中:

dealWithFiles(event) {
    files = event.originalEvent.files;
    // Deal with your files
    // e.g  assign it to a variable, and on submit add the variable to your form data
}

因为你想手动上传(通过另一个按钮),然后通过添加隐藏默认上传按钮:

<p-fileUpload name="files" showUploadButton="false" (onSelect)="dealWithFiles($event)"></p-fileUpload>

关于angular - PrimeNG - 通过单击按钮发送表单数据和 FileUpload 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52074193/

相关文章:

javascript - 通过@Input()传递的数据

jquery - 通过ajax传递Blob生成文件

html - 无法从父组件打开子组件中的表单 - 'cannot read property xxx'

typescript - 将 i18next 与 TypeScript 结合使用 : How to Support Prefixed Namespace Keys in the t Function

Angular 2 : How do you run 2 animations in parallel?

具有隐式属性类型的 Typescript 泛型

asp.net - 我无权访问的模块中的 HttpContext.Current.Session 为 null

drupal - 如何使用表单在 Drupal 7 中上传文件?

Angular - 从 URL 获取组件名称

Angular i18 国际化,以编程方式更改语言