PrimeNG之FileUpload

--文件上传是一个支持拖放,多文件上传,自动上传,进度跟踪和验证的上传。

Import

import {FileUploadModule} from ‘primeng/primeng‘;

Getting Started

文件上传需要一个URL属性为上传目标和一个名称来标识在后端的文件。

<p-fileUpload name="myfile[]" url="http://localhost:3000/upload"></p-fileUpload>

Multiple Uploads

只有一个文件可以同时被选择,允许选择倍数启用多个选项

<p-fileUpload name="myfile[]" url="http://localhost:3000/upload" multiple="multiple"></p-fileUpload>

DragDrop

文件选择也可以通过拖放一个或多个到组件的内容部分来完成。

Auto Uploads

启用自动属性后,一旦文件选择完成或文件在下拉区域被拖放,上传将开始。

<p-fileUpload name="myfile[]" url="http://localhost:3000/upload" multiple="multiple"
        accept="image/*" auto="auto"></p-fileUpload>

File Types

可选的文件类型可以接受属性限制,下面的例子只允许上传图片

<p-fileUpload name="myfile[]" url="http://localhost:3000/upload" multiple="multiple"
        accept="image/*"></p-fileUpload>

File Size

最大文件大小可以限制使用MAXFILESIZE属性定义的字节数。

<p-fileUpload name="myfile[]" url="http://localhost:3000/upload" multiple="multiple"
        accept="image/*" maxFileSize="1000000"></p-fileUpload>

为了自定义默认消息使用invalidfilesizemessagesummary和invalidfilesizemessagedetail选项。总之{ 0}占位符是指文件名和详细的文件大小。

  • invalidFileSizeMessageSummary: ‘{0}: Invalid file size, ‘          --(“{ 0 }:无效的文件大小,”)
  • invalidFileSizeMessageDetail: string = ‘maximum upload size is {0}.‘     --(字符串的最大上传大小是{ 0 })

Templating

文件列表UI是可定制的使用ng-template称为“file”,得到的文件实例作为隐式变量。命名为“内容”的第二个NG模板可用于将自定义内容放置在内容节中,这将有助于实现用户界面管理上传的文件,例如删除它们。第三和最后NG模板选项是“toolbar”,以显示自定义内容工具栏。

<p-fileUpload name="myfile[]" url="http://localhost:3000/upload" multiple="multiple"
        accept="image/*" maxFileSize="1000000">
        <ng-template pTemplate="toolbar">
            <div>Upload 3 Files</div>
        </ng-template>
        <ng-template let-file pTemplate="file">
            <div>Custom UI to display a file</div>
        </ng-template>
        <ng-template pTemplate="content">
            <div>Custom UI to manage uploaded files</div>
        </ng-template>
</p-fileUpload>

Request Customization

XHR请求上传文件可以定制使用onbeforeupload回调通过XHR实例和表单对象作为事件参数。

Attributes

Name  Type Default Description
name string null Name of the request parameter to identify the files at backend.
url string null Remote url to upload the files.
multiple boolean false Used to select multiple files at once from file dialog.
accept string false Pattern to restrict the allowed file types such as "image/*".
disabled boolean false Disables the upload functionality.
auto boolean false When enabled, upload begins automatically after selection is completed.
maxFileSize number null Maximum file size allowed in bytes.
invalidFileSizeMessageSummary string "{0}: Invalid file size, " Summary message of the invalid fize size.
invalidFileSizeMessageDetail string "maximum upload size is {0}." Detail message of the invalid fize size.
invalidFileTypeMessageSummary string "{0}: Invalid file type, " Summary message of the invalid fize type.
invalidFileTypeMessageDetail string "allowed file types: {0}." Detail message of the invalid fize type.
style string null Inline style of the component.
styleClass string null Style class of the component.
previewWidth number 50 Width of the image thumbnail in pixels.
chooseLabel string Choose Label of the choose button.
uploadLabel string Upload Label of the upload button.
cancelLabel string Cancel Label of the cancel button.

Events

 Name Parameters Description
onBeforeUpload event.xhr: XmlHttpRequest instance.
event.formData: FormData object.
Callback to invoke before file upload begins to customize the request
such as post parameters before the files.
onBeforeSend event.xhr: XmlHttpRequest instance.
event.formData: FormData object.
Callback to invoke before file send begins to customize the request
such as adding headers.
onUpload event.xhr: XmlHttpRequest instance.
event.files: Uploaded files.
Callback to invoke when file upload is complete.
onError event.xhr: XmlHttpRequest instance.
event.files: Files that are not uploaded.
Callback to invoke if file upload fails.
onClear -. Callback to invoke when files in queue are removed without uploading.
onSelect event.originalEvent: Original browser event.
event.files: List of selected files.
Callback to invoke when file upload is complete.

Styling

以下是结构式的类列表,对于主题类主题页面访问。

 Name Element
ui-fileupload Container element
ui-fileupload-buttonbar Header containing the buttons
ui-fileupload-content Content section

demo code

export class FileUploadDemo {

    msgs: Message[];

    uploadedFiles: any[] = [];

    onUpload(event) {
        for(let file of event.files) {
            this.uploadedFiles.push(file);
        }

        this.msgs = [];
        this.msgs.push({severity: ‘info‘, summary: ‘File Uploaded‘, detail: ‘‘});
    }
}

FileUploadDemo.ts

<p-growl [value]="msgs"></p-growl>

<p-fileUpload name="demo[]" url="http://localhost:3000/upload" (onUpload)="onUpload($event)"
        multiple="multiple" accept="image/*" maxFileSize="1000000">
    <ng-template pTemplate type="content">
        <ul *ngIf="uploadedFiles.length">
            <li *ngFor="let file of uploadedFiles">{{file.name}} - {{file.size}} bytes</li>
        </ul>
    </ng-template>
</p-fileUpload>

FileUploadDemo .html

参考资料

https://www.primefaces.org/primeng/#/fileupload

时间: 2024-10-20 22:51:18

PrimeNG之FileUpload的相关文章

Apache Commons FileUpload

1    概述 Commons FileUpdate包很容易为你的Servlet和web应用程序添加健壮的.高性能的文件上传功能.FileUpload解析遵循RFC 1876(在HTML中基于表单的文件上传)HTTP请求.即,如果一个HTTP请求使用POST方法提交,并且使用"multipart/form-data"的内容类型,然后FileUpload解析请求,使结果易于调用者使用.从1.3开始,FileUpload处理RFC 2047编码头值. 2    用户指南 2.1    使用

FileUpload上传总结

1. 文件上传写Fileupload代码时导包的是commons,而不是apach包 2. 创建一个工厂DiskFileItemFactory factory=new DiskFileItemFactory(); 将请求消息尸体中的每一个项目封装成单独的DiskFileitem对象 3. 创建解析文件上传的核心对象 ServletFileUpload upload=new ServletFileUpload(factory); 4. 解析有客户端通过request上传的请求: List<File

The import org.apache.commons.fileupload cannot be resolved

1.右键项目--Build Path--Add External Archives 2.选中需要引入的jar包(注意哦,不是zip文件) 3.确认添加外部文档 4.web App Libraries显示加载成功,即可引入 org.apache.commons.fileUpload文件

CVE-2014-0050: Exploit with Boundaries, Loops without Boundaries、Apache Commons FileUpload and Apache Tomcat DoS

catalog 1. Description 2. Analysis 3. POC 4. Solution 1. Description MultipartStream.java in Apache Commons FileUpload before 1.3.1, as used in Apache Tomcat, JBoss Web, and other products, allows remote attackers to cause a denial of service (infini

标准控件(三)——FileUpload

FileUpload 1.上传到硬盘文件夹 1.最简单的上传 HTML代码 <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /> C#代码 protected vo

简单的FileUpload文件上传

引入jar包:commons-fileupload-1.3.1.jar,commons-io-1.3.2.jar 前台HTML: <form action="./upload" method="post" enctype="multipart/form-data"> <table> <tr> <td><label>用户名:</label></td> <td&

fileupload控件在ajax中无法使用

google得到的方法: 1.http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx There is a better way of doing it with Ajax Control Toolkit version 3.0.30930 which works with .NET 3.5 SP1 and Visual Studio 2008 SP1. 

使用Commons FileUpLoad组件实现文件上传功能

Commons 是Apache开放的源码组织的一个java子项目,该项目主要涉及一些开发中常用的模块,如文件上传,命令行处理,数据库连接池等.FileUpLoad就是其中的一个用于处理HTTP文件上传的子项目.Commons FileUpLoad组建具有以下几个特点: 1.使用简单:Commons FileUpLoad组件可以方便的嵌入JSP文件中,在JSP文件中仅编写少量代码就可完成文件上传功能,十分方便. 2.能够全程控制上传的内容:使用Commons FileUpLoad组件提供的对象及操

使用Anthem.NET 1.5中的FileUpload控件实现Ajax方式的文件上传

Anthem.NET刚刚发布了其最新的1.5版本,其中很不错的一个新功能就是对文件上传功能的Ajax实现.本文将简要介绍一下该功能的使用方法. Anthem.NET的下载与安装 Anthem.NET可以在此下载:http://sourceforge.net/project/showfiles.php?group_id=151897&package_id=168043&release_id=493609 下载之后解压缩至硬盘中的某一目录中,编译项目得到Anthem.dll.然后将其拷贝到We