Files
ojnext/src/utils/download.ts
2023-11-01 19:03:01 +08:00

24 lines
603 B
TypeScript

import axios from "axios"
const http = axios.create({
baseURL: "/api/admin",
responseType: "blob",
xsrfHeaderName: "X-CSRFToken",
xsrfCookieName: "csrftoken",
})
async function download(url: string) {
const res = await http.get(url)
const headers = res.headers
const link = document.createElement("a")
link.href = window.URL.createObjectURL(
new window.Blob([res.data], { type: headers["content-type"] }),
)
link.download = (headers["content-disposition"] || "").split("filename=")[1]
document.body.appendChild(link)
link.click()
link.remove()
}
export default download