InputImage 图片

图片格式输入,需要实现接收器,提交时将以 url 的方式提交,如果需要以表单方式提交请使用 InputFile

基本用法

```schema: scope=”body” { “type”: “form”, “api”: “/api/mock2/form/saveForm”, “body”: [ { “type”: “input-image”, “name”: “image”, “label”: “image”, “receiver”: “/api/upload/file” } ] }

  1. 默认情况下,选中文件后,就会自动调用 `receiver` 配置里的接口进行上传,比如 node 版本的示例如下:
  2. ```javascript
  3. const express = require('express');
  4. const multer = require('multer');
  5. const upload = multer({dest: 'uploads/'});
  6. const app = express();
  7. app.use(express.static('public'));
  8. // 默认情况下是 file 字段名作为文件参数,也可以通过 fileField 配置来改成别的名字
  9. app.post('/uploader', upload.single('file'), function (req, res, next) {
  10. res.json({
  11. status: 0,
  12. data: {
  13. value: '/' + req.file.path
  14. }
  15. });
  16. });
  17. // 配合上面的返回值,将 uploads 目录可读,这样返回的文件才能正常显示
  18. app.get('uploads', express.static('uploads'));
  19. app.listen(8080, function () {});

这个接口需要返回图片地址,比如下面的格式

  1. {
  2. "status": 0,
  3. "msg": "",
  4. "data": {
  5. "value": "https:/xxx.yy/zz.png"
  6. }
  7. }

点击表单提交的时候,实际提交的就是这个返回的图片地址,比如上面的例子是 image,则会提交

  1. {
  2. "image": "https:/xxx.yy/zz.png"
  3. }

限制文件类型

可以配置accept来限制可选择的文件类型,格式是文件后缀名.xxx

```schema: scope=”body” { “type”: “form”, “api”: “/api/mock2/form/saveForm”, “body”: [ { “type”: “input-image”, “name”: “image”, “label”: “限制只能上传jpg图片”, “accept”: “.jpg”, “receiver”: “/api/upload/file” } ] }

  1. 想要限制多个类型,则用逗号分隔,例如:`.jpg,.png`
  2. ## 限制文件大小
  3. 配置 `limit`,更多属性请参考后面的属性说明。
  4. ```schema: scope="body"
  5. {
  6. "type": "form",
  7. "api": "/api/mock2/form/saveForm",
  8. "body": [
  9. {
  10. "type": "input-image",
  11. "name": "image",
  12. "label": "限制只能上传宽度大于 1000 的图片",
  13. "accept": ".jpg",
  14. "limit": {
  15. "minWidth": 1000
  16. },
  17. "receiver": "/api/upload/file"
  18. }
  19. ]
  20. }

支持裁剪

```schema: scope=”body” { “type”: “form”, “api”: “/api/mock2/form/saveForm”, “body”: [ { “type”: “input-image”, “name”: “image”, “label”: “上传后裁剪”, “receiver”: “/api/upload/file”, “crop”: true } ] }

  1. 设置裁剪比例等配置,具体细节可以参考[这里](https://github.com/fengyuanchen/cropperjs#options)。
  2. ```schema: scope="body"
  3. {
  4. "type": "form",
  5. "api": "/api/mock2/form/saveForm",
  6. "body": [
  7. {
  8. "type": "input-image",
  9. "name": "image",
  10. "label": "上传后裁剪",
  11. "receiver": "/api/upload/file",
  12. "crop": {
  13. "aspectRatio": 1.7777
  14. }
  15. }
  16. ]
  17. }

默认情况下裁剪结果是 png 格式,如果要支持其它格式,请设置 cropFormat,比如下面设置为 jpeg 格式,同时设置质量为 0.9

1.4.0 及以上版本

```schema: scope=”body” { “type”: “form”, “api”: “/api/mock2/form/saveForm”, “body”: [ { “type”: “input-image”, “name”: “image”, “label”: “上传后裁剪”, “receiver”: “/api/upload/file”, “crop”: true, “cropFormat”: “image/jpeg”, “cropQuality”: 0.9 } ] }

  1. 如果浏览器支持,还能设置为 `image/webp`
  2. ## 自动填充
  3. 上传成功后,可以通过配置 `autoFill` 将上传接口返回的值填充到某个表单项中:
  4. ```schema: scope="body"
  5. {
  6. "type": "form",
  7. "api": "/api/mock2/form/saveForm",
  8. "body": [
  9. {
  10. "type": "input-image",
  11. "name": "image",
  12. "label": "image",
  13. "receiver": "/api/upload/file",
  14. "autoFill": {
  15. "myUrl": "${url}"
  16. }
  17. },
  18. {
  19. "type": "text",
  20. "name": "myUrl",
  21. "label": "url"
  22. }
  23. ]
  24. }

上例中,image 组件上传后,接口返回格式例如如下:

  1. {
  2. "status": 0,
  3. "msg": "",
  4. "data": {
  5. "value": "xxxxxxx",
  6. "filename": "xxxx.jpg",
  7. "url": "http://xxxx.xxx.xxx"
  8. }
  9. }

然后 image 上配置:

  1. "autoFill": {
  2. "myUrl": "${url}"
  3. }

这样上传成功后,会把接口中的 url 变量,赋值给 myUrl 变量,这个里支持表达式,比如在前面加上域名

  1. "autoFill": {
  2. "myUrl": "https://cdn.com/${filename}"
  3. }

多选模式

当表单项为多选模式时,不能再直接取选项中的值了,而是通过 items 变量来取,通过它可以获取当前选中的选项集合。

```schema: scope=”body” { “type”: “form”, “debug”: true, “api”: “/api/mock2/form/saveForm”, “body”: [ { “type”: “input-image”, “name”: “image”, “label”: “image”, “multiple”: true, “receiver”: “/api/upload/file”, “autoFill”: { “myUrl”: “${items|pick:url}”, “lastUrl”: “${items|last|pick:url}” } } ] }

  1. **initAutoFill**
  2. 当表单反显时,可通过`initAutoFill`控制`autoFill`在数据反显时是否执行。
  3. ```schema: scope="body"
  4. {
  5. type: 'crud',
  6. api: '/api/mock2/crud/list',
  7. perPage: 3,
  8. columns: [
  9. {
  10. type: 'operation',
  11. label: '操作',
  12. buttons: [
  13. {
  14. type: 'button',
  15. label: '修改',
  16. level: 'link',
  17. size: 'xs',
  18. actionType: 'dialog',
  19. dialog: {
  20. title: '修改',
  21. size: 'lg',
  22. body: {
  23. "type": "form",
  24. "horizontal": {
  25. "left": 3,
  26. "right": 9
  27. },
  28. "api": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm",
  29. "body": [
  30. {
  31. "type": "input-image",
  32. "name": "image",
  33. "label": "image",
  34. "receiver": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/upload/file",
  35. "autoFill": {
  36. "text": "${url}"
  37. },
  38. "initAutoFill": true
  39. },
  40. {
  41. "type": "input-text",
  42. "name": "text",
  43. "label": "文本",
  44. },
  45. {
  46. "type": "input-image",
  47. "name": "carousel",
  48. "label": "image",
  49. "receiver": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/upload/file",
  50. "autoFill": {
  51. "id": "${url}"
  52. },
  53. "initAutoFill": false
  54. },
  55. {
  56. "type": "input-text",
  57. "name": "id",
  58. "label": "ID",
  59. },
  60. ]
  61. }
  62. },
  63. },
  64. ]
  65. },
  66. {
  67. name: 'id',
  68. label: 'ID',
  69. type: 'text'
  70. },
  71. {
  72. name: 'text',
  73. label: '文本',
  74. type: 'text'
  75. },
  76. {
  77. type: 'image',
  78. label: '图片',
  79. name: 'image',
  80. enlargeAble: true,
  81. title: '233',
  82. thumbMode: 'cover'
  83. },
  84. {
  85. name: 'carousel',
  86. label: '轮播图',
  87. type: 'carousel',
  88. width: '300'
  89. },
  90. ]
  91. }

属性表

除了支持 普通表单项属性表 中的配置以外,还支持下面一些配置

属性名 类型 默认值 说明
receiver API 上传文件接口
accept string .jpeg,.jpg,.png,.gif 支持的图片类型格式,请配置此属性为图片后缀,例如.jpg,.png
maxSize number 默认没有限制,当设置后,文件大小大于此值将不允许上传。单位为B
maxLength number 默认没有限制,当设置后,一次只允许上传指定数量文件。
multiple boolean false 是否多选。
joinValues boolean true 拼接值
extractValue boolean false 提取值
delimiter string , 拼接符
autoUpload boolean true 否选择完就自动开始上传
hideUploadButton boolean false 隐藏上传按钮
fileField string file 如果你不想自己存储,则可以忽略此属性。
crop boolean{"aspectRatio":""} 用来设置是否支持裁剪。
crop.aspectRatio number 裁剪比例。浮点型,默认 11:1,如果要设置 16:9 请设置 1.777777777777777716 / 9。。
crop.rotatable boolean false 裁剪时是否可旋转
crop.scalable boolean false 裁剪时是否可缩放
crop.viewMode number 1 裁剪时的查看模式,0 是无限制
cropFormat string image/png 裁剪文件格式
cropQuality number 1 裁剪文件格式的质量,用于 jpeg/webp,取值在 0 和 1 之间
limit Limit 限制图片大小,超出不让上传。
frameImage string 默认占位图地址
fixedSize boolean 是否开启固定尺寸,若开启,需同时设置 fixedSizeClassName
fixedSizeClassName string 开启固定尺寸时,根据此值控制展示尺寸。例如h-30,即图片框高为 h-30,AMIS 将自动缩放比率设置默认图所占位置的宽度,最终上传图片根据此尺寸对应缩放。
initAutoFill boolean false 表单反显时是否执行 autoFill

Limit 属性表

属性名 类型 默认值 说明
width number 限制图片宽度。
height number 限制图片高度。
minWidth number 限制图片最小宽度。
minHeight number 限制图片最小高度。
maxWidth number 限制图片最大宽度。
maxHeight number 限制图片最大高度。
aspectRatio number 限制图片宽高比,格式为浮点型数字,默认 11:1,如果要设置 16:9 请设置 1.777777777777777716 / 9。 如果不想限制比率,请设置空字符串。

事件表

当前组件会对外派发以下事件,可以通过onEvent来监听这些事件,并通过actions来配置执行的动作,在actions中可以通过event.data.xxx事件参数变量来获取事件产生的数据,详细请查看事件动作

事件名称 事件参数 说明
change event.data.file: Array<FileValue> 上传的文件 上传文件值变化时触发(上传失败同样会触发)
remove event.data.file: FileValue 被移除的文件 移除文件时触发
success event.data.file: FileValue 远程上传请求成功后返回的结果数据 上传成功时触发
fail event.data.file: FileValue 上传的文件
event.data.error: object 远程上传请求失败后返回的错误信息
上传文件失败时触发

FileValue 属性表

属性名 类型 说明
name string 图片名称
value string 上传成功后返回的 url
state string 文件当前状态,值可为 pending uploaded invalid
error string 错误信息

动作表

当前组件对外暴露以下特性动作,其他组件可以通过指定actionType: 动作名称componentId: 该组件id来触发这些动作,详细请查看事件动作

动作名称 动作配置 说明
clear - 清空