102 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { DownloadAnyFileDataType } from '@/types'
/**
*
* @param data PNG 图像的原始字节
*
* @returns PNG Data URL。
*/
export const arrayBufferToBase64Image = (data: ArrayBuffer) =>
'data:image/png;base64,' +
btoa(
Array.from(new Uint8Array(data), (byte) => String.fromCharCode(byte)).join(
'',
),
)
/**
*
* @param base64 待下载的 Data URL
* @param fileName 建议文件名
*
* @description
* 触发浏览器下载并清理临时链接。
*/
export const downloadBase64File = (base64: string, fileName: string) => {
const link = document.createElement('a')
link.href = base64
link.download = fileName
link.hidden = true
document.body.appendChild(link)
try {
link.click()
} finally {
link.remove()
}
}
/**
*
* @param data Data URL、Blob、File 或 ArrayBuffer
* @param fileName 建议文件名
*
* @description
* 发起浏览器下载请求,不等待磁盘写入。为二进制内容创建临时对象 URL,
* 并延迟释放,以便浏览器开始读取。
*
* @throws 数据类型不支持或下载链接无法触发时拒绝。
*/
export const downloadAnyFile = async (
data: DownloadAnyFileDataType,
fileName: string,
): Promise<void> => {
if (typeof data === 'string') {
downloadBase64File(data, fileName)
return
}
const blob =
data instanceof ArrayBuffer
? new Blob([data], { type: 'application/octet-stream' })
: data
if (!(blob instanceof Blob)) {
throw new TypeError('[downloadAnyFile]: Unsupported data type.')
}
const url = URL.createObjectURL(blob)
try {
downloadBase64File(url, fileName)
} catch (error) {
URL.revokeObjectURL(url)
throw error
}
// 锚点下载不会触发 load;留出时间给浏览器接管对象 URL。
setTimeout(() => URL.revokeObjectURL(url), 1000)
}
/**
*
* @param file 待读取的文件
*
* @returns 包含 MIME 类型的 Data URL。
*
* @throws 文件读取失败或中止时拒绝。
*/
export const fileToBase64 = (file: File) =>
new Promise<string>((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => resolve(reader.result as string)
reader.onerror = () => reject(reader.error)
reader.onabort = () =>
reject(new DOMException('File read aborted', 'AbortError'))
reader.readAsDataURL(file)
})