导出/下载 按钮
Posted Jay_帅小伙
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导出/下载 按钮相关的知识,希望对你有一定的参考价值。
import React from 'react'
import { Button, message } from 'antd'
import moment from 'moment'
export const ExportButton = (props) => {
const { service, isButton = true, downText = '导出', type = "primary", download } = props
const down = async () => {
if (!service) return message.warning('没有传入请求方法')
let res = await service()
if (res.status === 200 && res.data) {
let contentType = res.headers['content-type'];
if (contentType && contentType.indexOf('json') > 0) {//返回的是json
let enc = new TextDecoder('utf-8');
let res_err = JSON.parse(enc.decode(new Uint8Array(res.data)));
if (res_err.ret === 100016) {
message.info(res_err.msg)
} else {
message.error(res_err.msg);
}
} else {
let disposition = res.headers['content-disposition'];
let fileName = decodeURI(disposition.substring(disposition.indexOf('filename=') + 9, disposition.length));
let blob = new Blob([res.data], { type: 'application/msexcel' });
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
link.remove();
link.href = window.URL.revokeObjectURL(blob);
}
}
}
return (
<React.Fragment>
{
isButton ? <Button icon={props.icon || ''} onClick={down}>{downText}</Button> : <span onClick={down}>{downText}</span>
}
</React.Fragment>
)
}
<ExportButton icon="upload" service={expData} />
// 导出数据
@action expData = async () => {
let p = toJS(this.saveQueryParams)
const res = await axios.get(`${GLOBAL_API_DOMAIN}${expUrl}`, {
responseType: 'arraybuffer',
params: {
...p
}
})
return res
}
以上是关于导出/下载 按钮的主要内容,如果未能解决你的问题,请参考以下文章
[搬运] 将 Visual Studio 的代码片段导出到 VS Code