UNPKG

608 BJavaScriptView Raw
1import React from "react";
2import { DownloadOutlined } from "@ant-design/icons";
3
4export default function Download({ src, children = "文件下载" }) {
5 if (!src) return <div>--</div>;
6 return (
7 <a
8 href={src}
9 download={getFileName(src)}
10 style={{
11 color: "blue",
12 cursor: "pointer",
13 "&:hover": { color: "red" },
14 }}
15 >
16 <DownloadOutlined style={{ fontSize: "16px" }} /> {children}
17 </a>
18 );
19}
20
21const getFileName = src => {
22 // eslint-disable-next-line no-useless-escape
23 const match = src.match(/[^\/]+$/);
24 return match ? match[0] : src;
25};