feat: action 支持 saveAs 保存到本地功能 (#4362)

* feat: action 支持 saveAs 保存到本地功能

* 修复报错
This commit is contained in:
吴多益 2022-05-19 15:55:09 +08:00 committed by GitHub
parent d078fc1247
commit 380a6e4505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 0 deletions

View File

@ -354,6 +354,35 @@ Content-Disposition: attachment; filename="download.pdf"
Access-Control-Expose-Headers: Content-Disposition
```
## 保存到本地
> 1.10.0 及以上版本
和前面的下载接口功能类似,但不需要返回 `Content-Disposition` header只需要解决跨域问题主要用于一些简单的场景比如下载文本
```schema: scope="body"
{
"label": "保存",
"type": "action",
"actionType": "saveAs",
"api": "/api/download"
}
```
> 这个功能目前还没用到 env 里的 fetcher 方法,不支持 POST
默认会自动取 url 中的文件名,如果没有的话就需要指定,比如
```schema: scope="body"
{
"label": "保存",
"type": "action",
"actionType": "saveAs",
"fileName": "下载的文件名",
"api": "/api/download"
}
```
## 倒计时
主要用于发验证码的场景,通过设置倒计时 `countDown`(单位是秒),让点击按钮后禁用一段时间:

View File

@ -12,6 +12,9 @@ import {filter} from './utils/tpl';
import qs from 'qs';
import pick from 'lodash/pick';
import mapValues from 'lodash/mapValues';
import {saveAs} from 'file-saver';
import {normalizeApi} from './utils/api';
import {AjaxActionSchema} from './renderers/Action';
export interface RootRendererProps extends RootProps {
location?: any;
@ -218,6 +221,17 @@ export class RootRenderer extends React.Component<RootRendererProps> {
env.copy(filter(action.content || action.copy, ctx, '| raw'), {
format: action.copyFormat
});
} else if (action.actionType === 'saveAs') {
// 使用 saveAs 实现下载
// 不支持 env除非以后将 saveAs 代码拷过来改
const api = normalizeApi((action as AjaxActionSchema).api);
if (typeof api.url === 'string') {
let fileName = action.fileName || 'data.txt';
if (api.url.indexOf('.') !== -1) {
fileName = api.url.split('/').pop();
}
saveAs(api.url, fileName);
}
}
}

View File

@ -184,6 +184,14 @@ export interface DownloadActionSchema
actionType: 'download';
}
export interface SaveAsActionSchema
extends Omit<AjaxActionSchema, 'actionType'> {
/**
*
*/
actionType: 'saveAs';
}
export interface UrlActionSchema extends ButtonSchema {
/**
*

View File

@ -85,6 +85,7 @@ export interface Action extends Button {
| 'copy'
| 'reload'
| 'ajax'
| 'saveAs'
| 'dialog'
| 'drawer'
| 'jump'