fix: download not working
This commit is contained in:
parent
e32c22dc10
commit
27dbd26459
2 changed files with 21 additions and 2 deletions
|
|
@ -9,6 +9,24 @@ interface Props {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Card({ title, content, id, downloads }: Props) {
|
export default function Card({ title, content, id, downloads }: Props) {
|
||||||
|
function download(path: URL, fileName = 'file') {
|
||||||
|
fetch(path)
|
||||||
|
.then((response) => response.blob())
|
||||||
|
.then((blob) => {
|
||||||
|
const url = window.URL.createObjectURL(new Blob([blob]))
|
||||||
|
|
||||||
|
const link = document.createElement('a')
|
||||||
|
link.href = url
|
||||||
|
link.download = fileName
|
||||||
|
|
||||||
|
document.body.appendChild(link)
|
||||||
|
|
||||||
|
link.click()
|
||||||
|
|
||||||
|
link.parentNode?.removeChild(link)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="bg-[#161718] w-lg p-5 flex flex-col gap-5 rounded-xl self-center">
|
<div className="bg-[#161718] w-lg p-5 flex flex-col gap-5 rounded-xl self-center">
|
||||||
<h1 className="text-white text-2xl font-bold" id={id?.toString()}>
|
<h1 className="text-white text-2xl font-bold" id={id?.toString()}>
|
||||||
|
|
@ -24,10 +42,11 @@ export default function Card({ title, content, id, downloads }: Props) {
|
||||||
{downloads.map((e, i) => (
|
{downloads.map((e, i) => (
|
||||||
<li>
|
<li>
|
||||||
<a
|
<a
|
||||||
href={e.link}
|
href={e.link.toString()}
|
||||||
key={i}
|
key={i}
|
||||||
download={e.filename}
|
download={e.filename}
|
||||||
className="text-sky-600 underline"
|
className="text-sky-600 underline"
|
||||||
|
onClick={() => download(e.link, e.filename)}
|
||||||
>
|
>
|
||||||
{e.title}
|
{e.title}
|
||||||
</a>
|
</a>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
export type CardDownloadType = {
|
export type CardDownloadType = {
|
||||||
title: string
|
title: string
|
||||||
filename: string
|
filename: string
|
||||||
link: string
|
link: URL
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CardType = {
|
export type CardType = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue