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

Import

  1. import {FileUploadModule} from 'primeng/primeng';

Getting Started

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

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

Multiple Uploads

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

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

DragDrop

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

Auto Uploads

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

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

File Types

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

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

File Size

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

  1. <p-fileUpload name="myfile[]" url="http://localhost:3000/upload" multiple="multiple"
  2. 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”,以显示自定义内容工具栏。

  1. <p-fileUpload name="myfile[]" url="http://localhost:3000/upload" multiple="multiple"
  2. accept="image/*" maxFileSize="1000000">
  3. <ng-template pTemplate="toolbar">
  4. <div>Upload 3 Files</div>
  5. </ng-template>
  6. <ng-template let-file pTemplate="file">
  7. <div>Custom UI to display a file</div>
  8. </ng-template>
  9. <ng-template pTemplate="content">
  10. <div>Custom UI to manage uploaded files</div>
  11. </ng-template>
  12. </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

  1. export class FileUploadDemo {
  2.  
  3. msgs: Message[];
  4.  
  5. uploadedFiles: any[] = [];
  6.  
  7. onUpload(event) {
  8. for(let file of event.files) {
  9. this.uploadedFiles.push(file);
  10. }
  11.  
  12. this.msgs = [];
  13. this.msgs.push({severity: 'info', summary: 'File Uploaded', detail: ''});
  14. }
  15. }

FileUploadDemo.ts

  1. <p-growl [value]="msgs"></p-growl>
  2.  
  3. <p-fileUpload name="demo[]" url="http://localhost:3000/upload" (onUpload)="onUpload($event)"
  4. multiple="multiple" accept="image/*" maxFileSize="1000000">
  5. <ng-template pTemplate type="content">
  6. <ul *ngIf="uploadedFiles.length">
  7. <li *ngFor="let file of uploadedFiles">{{file.name}} - {{file.size}} bytes</li>
  8. </ul>
  9. </ng-template>
  10. </p-fileUpload>

FileUploadDemo .html

参考资料

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

PrimeNG之FileUpload的更多相关文章

  1. .JavaWeb文件上传和FileUpload组件使用

    .JavaWeb文件上传 1.自定义上传 文件上传时的表单设计要符合文件提交的方式: 1.提交方式:post 2.表单中有文件上传的表单项:<input type="file" ...

  2. C#-WebForm-文件上传-FileUpload控件

    FileUpload - 选择文件,不能执行上传功能,通过点击按钮实现上传 默认选择类型为所有类型 //<上传>按钮 void Button1_Click(object sender, E ...

  3. FileUpload组件

    package com.itheima.servlet; import java.io.File; import java.io.FileOutputStream; import java.io.IO ...

  4. 如何实现修改FileUpload样式

    这里先隐藏FileUpload 然后用一个input button和一个text来模拟FileUpload 具体代码为 <asp:FileUpload ID="FileUpload1& ...

  5. 上传文件fileupload

    文件上传: 需要使用控件-fileupload 1.如何判断是否选中文件? FileUpload.FileName -  选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.va ...

  6. fileupload图片预览功能

    FileUpload上传图片前首先预览一下 看看效果: 在专案中,创建aspx页面,拉上FileUpload控件一个Image,将用来预览上传时的图片. <%@ Page Language=&q ...

  7. textbox button 模拟fileupload

    方案一:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.asp ...

  8. FileUpload 上传文件,并实现c#使用Renci.SshNet.dll实现SFTP文件传输

    fileupload上传文件和jquery的uplodify控件使用方法类似,对服务器控件不是很熟悉,记录一下. 主要是记录新接触的sftp文件上传.服务器环境下使用freesshd搭建好环境后,wi ...

  9. C# 自定义FileUpload控件

    摘要:ASP.NET自带的FileUpload控件会随着浏览器的不同,显示的样式也会发生改变,很不美观,为了提高用户体验度,所以我们会去自定义FileUpload控件 实现思路:用两个Button和T ...

随机推荐

  1. 开发app前需要提前准备的资料

    需要准备的资料整理如下: 1 域名未注册,建议在 阿里云注册:https://www.aliyun.com/,2 服务器https://ecs-buy.aliyun.com/配置:计费方式:包年包月地 ...

  2. OpenCV 学习笔记 07 支持向量机SVM(flag)

    1 SVM 基本概念 本章节主要从文字层面来概括性理解 SVM. 支持向量机(support vector machine,简SVM)是二类分类模型. 在机器学习中,它在分类与回归分析中分析数据的监督 ...

  3. 关于 Docker Hub 上不能注册 Docker ID 的问题

    1. 引言 我们中国大陆访问dockerhub的时候,想要注册一个dockerID,发现sign up按钮是灰色的,不能点击进行注册.这个时候通过点击右键"查看网页源代码"和&qu ...

  4. Centos7.4修改主机名HostName颜色及格式

    一.打开 .bashrc文件 1.位置:~(cd ~)目录下 2.cat .bashrc 原文件内容如下: # .bashrc # User specific aliases and function ...

  5. OpenCV自带dnn的Example研究(4)— openpose

    这个博客系列,简单来说,今天我们就是要研究 https://docs.opencv.org/master/examples.html下的 6个文件,看看在最新的OpenCV中,它们是如何发挥作用的. ...

  6. Openfire 单人聊天和多人聊天(发送消息、接收消息)

    Openfire 单人聊天和多人聊天(发送消息.接收消息) 一.单人聊天 1)发送消息: 首先要获取一个聊天窗口,getConnection()为获取连接connection的方法,调用getFrie ...

  7. guid与Base64编码互相转换

    guid的长度比较长,本文提供一种方法,将guid转为base64字符串,只有22位长度,比较好! 参考:https://blog.csdn.net/tgghfbflishuai/article/de ...

  8. Js 动态添加的数据,监听事件监听不到

    在开发中遇到这种问题,就是有些数据,比如按钮是动态添加进去的,结果添加事件监听无效,直接写死在页面上是可以的. 这就是很明显的加载先后顺序的问题了. 解决的方法: $(document).ready( ...

  9. Unity调用安卓Android的Toast

    需求:在游戏中弹窗消息,调起安卓的Toast 项目中需要做Unity和安卓交互时,经常需要通过安卓Toast来做简单的输出,以便于测试. 方法一:Unity中,C#主导 // Unity调用安卓的土司 ...

  10. 外盘持仓盈亏何时推送---ITapTradeAPINotify::OnRtnPositionProfit

    易盛外盘提供了一个可以直接获取持仓盈亏的函数,这个比CTP方便多了 virtual void TAP_CDECL ITapTrade::ITapTradeAPINotify::OnRtnPositio ...