dark
7 months ago
5 changed files with 88 additions and 11 deletions
-
58src/components/download/Download.tsx
-
0src/components/download/index.ts
-
11src/pages/websites/ssl/index.tsx
-
18src/request.ts
-
6src/service/websites.ts
@ -0,0 +1,58 @@ |
|||
import React, { useEffect, useRef, useState } from 'react' |
|||
import { LoadingOutlined } from '@ant-design/icons' |
|||
|
|||
export interface DownloadProps { |
|||
children?: React.ReactNode; |
|||
className?: string; |
|||
style?: React.CSSProperties; |
|||
server?: () => Promise<any> |
|||
fileName?: string |
|||
|
|||
[key: string]: any |
|||
} |
|||
|
|||
const Download = ({ server, style, className, fileName, children, ...props }: DownloadProps) => { |
|||
|
|||
const [ isLoading, setLoading ] = useState(false) |
|||
const serverRef = useRef(server) |
|||
serverRef.current = server |
|||
|
|||
useEffect(() => { |
|||
serverRef.current = server |
|||
}, [ server ]) |
|||
|
|||
return ( |
|||
<div className={className} |
|||
style={style} {...props} |
|||
|
|||
onClick={() => { |
|||
if (isLoading) return |
|||
serverRef.current && serverRef.current()?.then(res => { |
|||
|
|||
if (!res) { |
|||
return res |
|||
} |
|||
|
|||
const downloadUrl = window.URL.createObjectURL(new Blob([ res ])) |
|||
const a = document.createElement('a') |
|||
a.style.display = 'none' |
|||
a.href = downloadUrl |
|||
a.download = fileName || '' |
|||
const event = new MouseEvent('click') |
|||
a.dispatchEvent(event) |
|||
window.URL.revokeObjectURL(downloadUrl) |
|||
setTimeout(() => a.remove()) |
|||
return res |
|||
})?.finally(() => { |
|||
setLoading(false) |
|||
|
|||
}) |
|||
}} |
|||
> |
|||
{isLoading && <LoadingOutlined/>} |
|||
{children} |
|||
</div> |
|||
) |
|||
} |
|||
|
|||
export default Download |
Write
Preview
Loading…
Cancel
Save
Reference in new issue