2020-07-28 10:03:53 +08:00
|
|
|
|
---
|
|
|
|
|
title: File 文件上传
|
2020-07-29 16:20:21 +08:00
|
|
|
|
description:
|
2020-07-28 10:03:53 +08:00
|
|
|
|
type: 0
|
|
|
|
|
group: null
|
|
|
|
|
menuName: File
|
2020-07-29 16:20:21 +08:00
|
|
|
|
icon:
|
2020-07-28 10:03:53 +08:00
|
|
|
|
order: 21
|
|
|
|
|
---
|
2020-07-29 16:20:21 +08:00
|
|
|
|
|
2020-07-28 10:03:53 +08:00
|
|
|
|
## 基本用法
|
|
|
|
|
|
|
|
|
|
用来负责文件上传,文件上传成功后会返回文件地址,这个文件地址会作为这个表单项的值,整个表单提交的时候,其实提交的是文件地址,文件上传已经在这个控件中完成了。
|
|
|
|
|
|
2021-01-07 23:35:03 +08:00
|
|
|
|
```schema: scope="body"
|
2020-07-28 10:03:53 +08:00
|
|
|
|
{
|
|
|
|
|
"type": "form",
|
2021-01-19 10:20:09 +08:00
|
|
|
|
"api": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm",
|
2020-07-28 10:03:53 +08:00
|
|
|
|
"controls": [
|
|
|
|
|
{
|
|
|
|
|
"type": "file",
|
|
|
|
|
"name": "file",
|
|
|
|
|
"label": "File",
|
|
|
|
|
"accept": "*",
|
2021-01-27 17:37:22 +08:00
|
|
|
|
"receiver": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/upload/file"
|
2020-07-28 10:03:53 +08:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-10-30 16:46:06 +08:00
|
|
|
|
### 接口返回格式
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"status": 0,
|
|
|
|
|
"msg": "",
|
|
|
|
|
"data": {
|
|
|
|
|
"value": "xxxx"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- value:必须返回该字段用作回显,一般是文件资源地址
|
|
|
|
|
|
2020-07-28 10:03:53 +08:00
|
|
|
|
## 限制文件类型
|
|
|
|
|
|
|
|
|
|
可以配置`accept`来限制可选择的文件类型,格式是文件后缀名`.xxx`
|
|
|
|
|
|
2021-01-07 23:35:03 +08:00
|
|
|
|
```schema: scope="body"
|
2020-07-28 10:03:53 +08:00
|
|
|
|
{
|
|
|
|
|
"type": "form",
|
2021-01-19 10:20:09 +08:00
|
|
|
|
"api": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm",
|
2020-07-28 10:03:53 +08:00
|
|
|
|
"controls": [
|
|
|
|
|
{
|
|
|
|
|
"type": "file",
|
|
|
|
|
"name": "file",
|
|
|
|
|
"label": "限制只能上传csv文件",
|
|
|
|
|
"accept": ".csv",
|
2021-01-27 17:37:22 +08:00
|
|
|
|
"receiver": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/upload/file"
|
2020-07-28 10:03:53 +08:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
想要限制多个类型,则用逗号分隔,例如:`.csv,.md`
|
|
|
|
|
|
|
|
|
|
## 手动上传
|
|
|
|
|
|
|
|
|
|
如果不希望 File 组件上传,可以配置 `asBlob` 或者 `asBase64`,采用这种方式后,组件不再自己上传了,而是直接把文件数据作为表单项的值,文件内容会在 Form 表单提交的接口里面一起带上。
|
|
|
|
|
|
2021-01-07 23:35:03 +08:00
|
|
|
|
```schema: scope="body"
|
2020-07-28 10:03:53 +08:00
|
|
|
|
{
|
|
|
|
|
"type": "form",
|
2021-01-19 10:20:09 +08:00
|
|
|
|
"api": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm",
|
2020-07-28 10:03:53 +08:00
|
|
|
|
"debug": true,
|
|
|
|
|
"controls": [
|
|
|
|
|
{
|
|
|
|
|
"type": "file",
|
|
|
|
|
"name": "file",
|
|
|
|
|
"label": "File",
|
|
|
|
|
"accept": "*",
|
|
|
|
|
"asBlob": true
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
上例中,选择任意文件,然后观察数据域变化;点击提交,amis 自动会调整接口数据格式为`FormData`
|
|
|
|
|
|
|
|
|
|
## 分块上传
|
|
|
|
|
|
|
|
|
|
如果文件过大,则可能需要使用分块上传
|
|
|
|
|
|
2020-11-13 15:21:41 +08:00
|
|
|
|
## 自动填充
|
|
|
|
|
|
|
|
|
|
上传成功后,可以通过配置 `autoFill` 将上传接口返回的值填充到某个表单项中:
|
|
|
|
|
|
2021-01-07 23:35:03 +08:00
|
|
|
|
```schema: scope="body"
|
2020-11-13 15:21:41 +08:00
|
|
|
|
{
|
|
|
|
|
"type": "form",
|
2021-01-19 10:20:09 +08:00
|
|
|
|
"api": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm",
|
2020-11-13 15:21:41 +08:00
|
|
|
|
"controls": [
|
|
|
|
|
{
|
|
|
|
|
"type": "file",
|
|
|
|
|
"name": "file",
|
|
|
|
|
"label": "File",
|
|
|
|
|
"accept": "*",
|
2021-01-27 17:37:22 +08:00
|
|
|
|
"receiver": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/upload/file",
|
2020-11-13 15:21:41 +08:00
|
|
|
|
"autoFill": {
|
|
|
|
|
"myUrl": "${url}"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "text",
|
|
|
|
|
"name": "myUrl",
|
|
|
|
|
"label": "url"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
上例中,file 组件上传后,接口返回格式例如如下:
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2020-12-24 16:18:52 +08:00
|
|
|
|
"status": 0,
|
|
|
|
|
"msg": "",
|
|
|
|
|
"data": {
|
|
|
|
|
"value": "xxxxxxx",
|
|
|
|
|
"filename": "xxxx.csv",
|
|
|
|
|
"url": "http://xxxx.xxx.xxx"
|
|
|
|
|
}
|
2020-11-13 15:21:41 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
然后 file 上配置:
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
"autoFill": {
|
|
|
|
|
"myUrl": "${url}"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这样上传成功后,会把接口中的 `url` 变量,赋值给 `myUrl` 变量
|
|
|
|
|
|
2020-07-28 10:03:53 +08:00
|
|
|
|
## 属性表
|
|
|
|
|
|
|
|
|
|
除了支持 [普通表单项属性表](./formitem#%E5%B1%9E%E6%80%A7%E8%A1%A8) 中的配置以外,还支持下面一些配置
|
|
|
|
|
|
2020-07-29 17:33:14 +08:00
|
|
|
|
| 属性名 | 类型 | 默认值 | 说明 |
|
|
|
|
|
| ---------------- | ---------------------- | ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
|
2021-01-27 17:37:22 +08:00
|
|
|
|
| receiver | [API](../../types/api) | | 上传文件接口 |
|
2020-07-29 17:33:14 +08:00
|
|
|
|
| accept | `string` | `text/plain` | 默认只支持纯文本,要支持其他类型,请配置此属性为文件后缀`.xxx` |
|
|
|
|
|
| asBase64 | `boolean` | `false` | 将文件以`base64`的形式,赋值给当前组件 |
|
|
|
|
|
| asBlob | `boolean` | `false` | 将文件以二进制的形式,赋值给当前组件 |
|
2020-09-24 10:51:46 +08:00
|
|
|
|
| maxSize | `number` | | 默认没有限制,当设置后,文件大小大于此值将不允许上传。单位为`KB` |
|
2020-07-29 17:33:14 +08:00
|
|
|
|
| maxLength | `number` | | 默认没有限制,当设置后,一次只允许上传指定数量文件。 |
|
|
|
|
|
| multiple | `boolean` | `false` | 是否多选。 |
|
|
|
|
|
| joinValues | `boolean` | `true` | [拼接值](./options#%E6%8B%BC%E6%8E%A5%E5%80%BC-joinvalues) |
|
|
|
|
|
| extractValue | `boolean` | `false` | [提取值](./options#%E6%8F%90%E5%8F%96%E5%A4%9A%E9%80%89%E5%80%BC-extractvalue) |
|
2020-11-19 17:36:29 +08:00
|
|
|
|
| delimiter | `string` | `,` | [拼接符](./options#%E6%8B%BC%E6%8E%A5%E7%AC%A6-delimiter) |
|
2020-07-29 17:33:14 +08:00
|
|
|
|
| autoUpload | `boolean` | `true` | 否选择完就自动开始上传 |
|
|
|
|
|
| hideUploadButton | `boolean` | `false` | 隐藏上传按钮 |
|
|
|
|
|
| stateTextMap | object | `{ init: '', pending: '等待上传', uploading: '上传中', error: '上传出错', uploaded: '已上传', ready: '' }` | 上传状态文案 |
|
|
|
|
|
| fileField | `string` | `file` | 如果你不想自己存储,则可以忽略此属性。 |
|
2021-01-21 18:13:04 +08:00
|
|
|
|
| btnLabel | `string` | | 上传按钮的文字 |
|
2020-07-29 17:33:14 +08:00
|
|
|
|
| downloadUrl | `boolean`或`string` | `""` | 默认显示文件路径的时候会支持直接下载,可以支持加前缀如:`http://xx.dom/filename=` ,如果不希望这样,可以把当前配置项设置为 `false`。 |
|
|
|
|
|
| useChunk | `boolean`或`"auto"` | `"auto"` | amis 所在服务器,限制了文件上传大小不得超出 10M,所以 amis 在用户选择大文件的时候,自动会改成分块上传模式。 |
|
|
|
|
|
| chunkSize | `number` | `5 * 1024 * 1024` | 分块大小 |
|
|
|
|
|
| startChunkApi | [API](../../types/api) | | startChunkApi |
|
|
|
|
|
| chunkApi | [API](../../types/api) | | chunkApi |
|
|
|
|
|
| finishChunkApi | [API](../../types/api) | | finishChunkApi |
|