Spaces:
Running
Running
import { GenericIdentityFn, IQueryErrorResponse } from "../api/query"; | |
import { AxiosResponseHeaders } from "axios"; | |
export const getContentFilename = (headers?: AxiosResponseHeaders) => { | |
const fileNameHeader = headers?.get("content-disposition")?.toString(); | |
const regex = /filename="([^"]+)"/; | |
const match = fileNameHeader?.match(regex); | |
return match?.[1]; | |
}; | |
export const downloadFile = ( | |
response: IQueryErrorResponse | GenericIdentityFn<Blob>, | |
filenameparam?: string | |
) => { | |
if ("data" in response && response.data) { | |
const url = URL.createObjectURL(response.data); | |
const link = document.createElement("a"); | |
link.href = url; | |
link.download = `${filenameparam ?? "file.pdf"}`; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
URL.revokeObjectURL(url); | |
} | |
}; | |