1
0
Fork 0
mirror of synced 2024-09-03 03:01:14 +12:00
budibase/packages/frontend-core/src/utils/download.js
2023-07-11 10:44:10 +02:00

36 lines
896 B
JavaScript

export function downloadText(filename, text) {
if (typeof text === "object") {
text = JSON.stringify(text)
}
const blob = new Blob([text], { type: "plain/text" })
const url = URL.createObjectURL(blob)
const link = document.createElement("a")
link.href = url
link.download = filename
link.click()
URL.revokeObjectURL(url)
}
export async function downloadStream(streamResponse) {
const blob = await streamResponse.blob()
const contentDisposition = streamResponse.headers.get("Content-Disposition")
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(
contentDisposition
)
const filename = matches[1].replace(/['"]/g, "")
const resBlob = new Blob([blob])
const blobUrl = URL.createObjectURL(resBlob)
const link = document.createElement("a")
link.href = blobUrl
link.download = filename
link.click()
URL.revokeObjectURL(blobUrl)
}