我的 REACT 应用程序中提供了文件 (pdf) 的名称和内容(作为 Base64 字符串)。现在,我想添加一个用于下载文件的按钮。因此,用户可以按下按钮,浏览器的下载对话框就会弹出。
我有什么想法可以实现这一点吗?我正在使用 REACT-bootstrap 所以我的按钮看起来像这样:
return(
<Button onClick={() => filedownload()}>
Download
</Button>
);
function filedownload() {
const filename = 'filename.txt';
const b63string = 'hereisthelongbase64string...';
// ???
}
有什么推荐吗?
提前致谢!
保持简单并没有什么错
const filename = 'filename.txt';
const b64string = 'hereisthelongbase64string...';
return (
<a
className="styled-like-a-button"
download={filename}
href={`data:application/pdf;base64,${b64string}`}
>Download</a>
);