You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
7.1 KiB

---
category: Components
subtitle: 上传
type: 数据录入
title: Upload
cover: https://gw.alipayobjects.com/zos/alicdn/QaeBt_ZMg/Upload.svg
---
9 years ago
文件选择上传和拖拽上传控件。
9 years ago
## 何时使用
上传是将信息(网页、文字、图片、视频等)通过网页或者上传工具发布到远程服务器上的过程。
9 years ago
- 当需要上传一个或一些文件时。
- 当需要展现上传的进度时。
- 当需要使用拖拽交互时。
9 years ago
## API
meger feature to master (#16421) * use ul in list * update snapshot * update comment * feat: TreeSelect support `showSearch` in multiple mode (#15933) * update rc-tree-select * typo * update desc & snapshot * update desc & snapshot * check default showSearch * feat: table customizing variable (#15971) * feat: added table selected row color variable * fix: @table-selected-row-color default is inherit * feat: Upload support customize previewFile (#15984) * support preview file * use promise * dealy load * use canvas of render * use domHook of test * update demo * add snapshot * update types * update testcase * feat: form customizing variables (#15954) * fix: added styling form input background-color * feat: added '@form-warning-input-bg' variable * feat: added '@form-error-input-bg' variable * use li wrap with comment * feat: Support append theme less file with less-variable (#16118) * add override * add override support * update doc * feat: dropdown support set right icon * docs: update doc of dropdown component * style: format dropdown-button.md * test: update updateSnapshot * style: format dropdown-button.md * test: update updateSnapshot * test: update updateSnapshot * style: change style of dropdown-button demo * fix: fix document table order * feat: Support SkeletonAvatarProps.size accept number (#16078) (#16128) * chore:update style of demo * feat: Notification functions accept top, bottom and getContainer as arguments * drawer: add afterVisibleChange * rm onVisibleChange * update * feat: 🇭🇷 hr_HR locale (#16258) * Added Croatian locale * fixed lint error * :white_check_mark: Add test cases for hr_HR * :memo: update i18n documentation * feat: add `htmlFor` in Form.Item (#16278) * add htmlFor in Form.Item * update doc * feat: Button support `link` type (#16289) close #15892 * feat: Add Timeline.Item.position (#16148) (#16193) * fix: Timeline.pendingDot interface documentation there is a small problem (#16177) * feat: Add Timeline.Item.position (#16148) * doc: add version infomation for Timeline.Item.position * refactor: Update Tree & TreeSelect deps (#16330) * use CSSMotion * update snapshot * feat: Collapse support `expandIconPosition` (#16365) * update doc * support expandIconPosition * update snapshot * feat: Breadcrumb support DropDown (#16315) * breadcrumbs support drop down menu * update doc * add require less * fix test * fix md doc * less code * fix style warning * update snap * add children render test * feat: TreeNode support checkable * feat: add optional to support top and left slick dots (#16186) (#16225) * add optional to support top and left slick dots * update carousel snapshot * Update doc, add placement demo * update carousel placement demo snapshots * rename dots placement to position * update vertical as deprecated * rename dotsPosition to dotPosition * refine code * add warning testcase for vertical * remove unused warning * update expression * Additional test case for dotPosition * refactor: Upgrade `rc-tree-select` to support pure React motion (#16402) * upgrade `rc-tree-select` * update snapshot * 3.17.0 changelog * fix warning * fix review warning
6 years ago
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| accept | 接受上传的文件类型, 详见 [input accept Attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept) | string | - | |
| action | 上传的地址 | string \| (file) => Promise<string> | - | |
| beforeUpload | 上传文件之前的钩子,参数为上传的文件,若返回 `false` 则停止上传。支持返回一个 Promise 对象,Promise 对象 reject 时则停止上传,resolve 时开始上传( resolve 传入 `File``Blob` 对象则上传 resolve 传入对象);也可以返回 `Upload.LIST_IGNORE`,此时列表中将不展示此文件。 **注意:IE9 不支持该方法** | (file, fileList) => boolean \| Promise<File> \| `Upload.LIST_IGNORE` | - | |
| customRequest | 通过覆盖默认的上传行为,可以自定义自己的上传实现 | function | - | |
| data | 上传所需额外参数或返回上传额外参数的方法 | object\|(file) => object \| Promise<object> | - | |
| defaultFileList | 默认已经上传的文件列表 | object\[] | - | |
| directory | 支持上传文件夹([caniuse](https://caniuse.com/#feat=input-file-directory)) | boolean | false | |
| disabled | 是否禁用 | boolean | false | |
| fileList | 已经上传的文件列表(受控),使用此参数时,如果遇到 `onChange` 只调用一次的问题,请参考 [#2423](https://github.com/ant-design/ant-design/issues/2423) | [UploadFile](#UploadFile)\[] | - | |
| headers | 设置上传的请求头部,IE10 以上有效 | object | - | |
| iconRender | 自定义显示 icon | (file: UploadFile, listType?: UploadListType) => ReactNode | - | |
| isImageUrl | 自定义缩略图是否使用 <img /> 标签进行显示 | (file: UploadFile) => boolean | [(内部实现)](https://github.com/ant-design/ant-design/blob/4ad5830eecfb87471cd8ac588c5d992862b70770/components/upload/utils.tsx#L47-L68) | |
| itemRender | 自定义上传列表项 | (originNode: ReactElement, file: UploadFile, fileList: object\[], actions: { download: function, preview: function, remove: function }) => React.ReactNode | - | 4.16.0 |
| listType | 上传列表的内建样式,支持三种基本样式 `text`, `picture``picture-card` | string | `text` | |
| maxCount | 限制上传数量。当为 1 时,始终用最新上传的文件代替当前文件 | number | - | 4.10.0 |
| method | 上传请求的 http method | string | `post` | |
| multiple | 是否支持多选文件,`ie10+` 支持。开启后按住 ctrl 可选择多个文件 | boolean | false | |
| name | 发到后台的文件参数名 | string | `file` | |
| openFileDialogOnClick | 点击打开文件对话框 | boolean | true | |
| previewFile | 自定义文件预览逻辑 | (file: File \| Blob) => Promise<dataURL: string> | - | |
| progress | 自定义进度条样式 | [ProgressProps](/components/progress/#API)(仅支持 `type="line"`) | { strokeWidth: 2, showInfo: false } | 4.3.0 |
| showUploadList | 是否展示文件列表, 可设为一个对象,用于单独设定 `showPreviewIcon`, `showRemoveIcon`, `showDownloadIcon`, `removeIcon``downloadIcon` | boolean \| { showPreviewIcon?: boolean, showRemoveIcon?: boolean, showDownloadIcon?: boolean, removeIcon?: ReactNode \| (file: UploadFile) => ReactNode, downloadIcon?: ReactNode \| (file: UploadFile) => ReactNode } | true | function: 4.7.0 |
| withCredentials | 上传请求时是否携带 cookie | boolean | false | |
| onChange | 上传文件改变时的状态,详见 [onChange](#onChange) | function | - | |
| onDrop | 当文件被拖入上传区域时执行的回调功能 | (event: React.DragEvent) => void | - | 4.16.0 |
| onDownload | 点击下载文件时的回调,如果没有指定,则默认跳转到文件 url 对应的标签页 | function(file): void | (跳转新标签页) | |
| onPreview | 点击文件链接或预览图标时的回调 | function(file) | - | |
| onRemove   | 点击移除文件时的回调,返回值为 false 时不移除。支持返回一个 Promise 对象,Promise 对象 resolve(false) 或 reject 时不移除               | function(file): boolean \| Promise | -   | |
### UploadFile
继承自 File,附带额外属性用于渲染。
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| name | 文件名 | string | - |
| percent | 上传进度 | number | - |
| status | 上传状态,不同状态展示颜色也会有所不同 | `error` \| `success` \| `done` \| `uploading` \| `removed` | - |
| thumbUrl | 缩略图地址 | string | - |
| uid | 唯一标识符,不设置时会自动生成 | string | - |
| url | 下载地址 | string | - |
### onChange
9 years ago
> 上传中、完成、失败都会调用这个函数。
文件状态改变的回调,返回为:
```js
{
9 years ago
file: { /* ... */ },
fileList: [ /* ... */ ],
event: { /* ... */ },
}
```
1. `file` 当前操作的文件对象。
```js
{
9 years ago
uid: 'uid', // 文件唯一标识,建议设置为负数,防止和内部产生的 id 冲突
name: 'xx.png' // 文件名
status: 'done', // 状态有:uploading done error removed,被 beforeUpload 拦截的文件没有 status 属性
response: '{"status": "success"}', // 服务端响应内容
linkProps: '{"download": "image"}', // 下载链接额外的 HTML 属性
}
```
2. `fileList` 当前的文件列表。
3. `event` 上传中的服务端响应内容,包含了上传进度等信息,高级浏览器支持。
## FAQ
### 服务端如何实现?
- 服务端上传接口实现可以参考 [jQuery-File-Upload](https://github.com/blueimp/jQuery-File-Upload/wiki#server-side)。
- 如果要做本地 mock 可以参考这个 [express 的例子](https://github.com/react-component/upload/blob/master/server.js)。
### 如何显示下载链接?
9 years ago
请使用 fileList 属性设置数组项的 url 属性进行展示控制。
### `customRequest` 怎么使用?
请参考 <https://github.com/react-component/upload#customrequest>
### 为何 `fileList` 受控时,上传不在列表中的文件不会触发 `onChange` 后续的 `status` 更新事件?
`onChange` 事件仅会作用于在列表中的文件,因而 `fileList` 不存在对应文件时后续事件会被忽略。请注意,在 `4.13.0` 版本之前受控状态存在 bug 导致不在列表中的文件也会触发。
merge Feature into master (#29758) * feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang &lt;chenliang9@xiaomi.com&gt; * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg &lt;xrkffgg@vip.qq.com&gt; * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg &lt;xrkffgg@vip.qq.com&gt; * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty &lt;zty.dev@outlook.com&gt; Co-authored-by: zty &lt;zty.dev@icloud.com&gt; Co-authored-by: xrkffgg &lt;xrkffgg@vip.qq.com&gt; Co-authored-by: github-actions[bot] &lt;41898282+github-actions[bot]@users.noreply.github.com&gt; Co-authored-by: jueinin &lt;1014397160@qq.com&gt; Co-authored-by: chenliang &lt;chenliang9@xiaomi.com&gt; Co-authored-by: 不吃猫的鱼 &lt;michael2ib1989@gmail.com&gt; Co-authored-by: 二货机器人 &lt;smith3816@gmail.com&gt; Co-authored-by: Tianyuan Zhang &lt;tianyuan233.zhang@gmail.com&gt; Co-authored-by: zty &lt;zty.dev@outlook.com&gt; Co-authored-by: zty &lt;zty.dev@icloud.com&gt; Co-authored-by: xrkffgg &lt;xrkffgg@vip.qq.com&gt;
4 years ago
### `onChange` 为什么有时候返回 File 有时候返回 { originFileObj: File }?
历史原因,在 `beforeUpload` 返回 `false` 时,会返回 File 对象。在下个大版本我们会统一返回 `{ originFileObj: File }` 对象。当前版本已经兼容所有场景下 `info.file.originFileObj` 获取原 File 写法。你可以提前切换。