amis上传组件导入文件接口参数为base64格式的使用示例
amis上传组件地址:
amis - 低代码前端框架-上传组件https://aisuda.bce.baidu.com/amis/zh-CN/components/form/input-file
ajax事件:
amis - 低代码前端框架-ajax事件https://aisuda.bce.baidu.com/amis/zh-CN/docs/concepts/event-action#%E5%8F%91%E9%80%81-http-%E8%AF%B7%E6%B1%82
找不到,可以搜索“ajax”,再继续搜索expression:
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
expression | boolean |表达式 | - | 执行条件,不设置表示默认执行 |
实现思路:
普通上传参照官方示例就行了,下方是base64格式文件自动上传的步骤:
- 上传组件,文件内容为base64格式,利用change事件实现自动上传
- silent为true静默调用接口,outputVar接收抛出接口返回的数据,存入变量res
- toast显示接口返回的提示信息res.responseMsg,使用expression控制显示条件,成功时不要显示成功信息,只在接口报错时显示
- 不过上传文件成功后再删除该文件,删除会被识别为change事件,使用expression让文件为空时不调用上传接口,避免出错
代码示例:
{"type": "input-file","id": "file-upload-btn","drag": false,"name": "xxx","label": "上传","proxy": true,"accept": ".xls,.xlsx","maxSize": 5000000,"onEvent": {"change": {"actions": [{"api": {"url": "/xxx/import","data": {"fileName": "${file.name}","base64Value": "${file.value}"},"method": "post","silent": true,"requestAdaptor": "return { ...api, data: { ...api.data,base64Value:api.data.base64Value.split(',')[1]}}"},"outputVar": "res","actionType": "ajax","expression": "${!!file.name}"},{"args": {"msg": "${res.responseMsg}","msgType": "error"},"actionType": "toast","expression": "${res.responseStatus!=0}"}]}},"asBase64": true,"btnLabel": "上传文件","multiple": false,"required": true,"useChunk": false,"autoUpload": true,"uploadType": "fileReceptor","hideUploadButton": true
}