1
0
Fork 0
mirror of synced 2024-09-15 16:59:43 +12:00
budibase/packages/frontend-core/src/utils/download.js

37 lines
896 B
JavaScript
Raw Normal View History

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)
}
2023-07-10 23:25:24 +12:00
2023-07-10 23:57:27 +12:00
export async function downloadStream(streamResponse) {
2023-07-10 23:25:24 +12:00
const blob = await streamResponse.blob()
2023-07-10 23:57:27 +12:00
const contentDisposition = streamResponse.headers.get("Content-Disposition")
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(
contentDisposition
)
const filename = matches[1].replace(/['"]/g, "")
2023-07-10 23:25:24 +12:00
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)
}