feat(api): add downloadBlob helper for blob attachment download
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -41,3 +41,23 @@ http.interceptors.response.use(
|
||||
export function getJson<T>(url: string, params?: Record<string, unknown>): Promise<T> {
|
||||
return http.get(url, { params }) as unknown as Promise<T>
|
||||
}
|
||||
|
||||
/** 以 blob 下载(用于 HTML 导出等二进制/文本附件下载)。返回建议文件名(来自 content-disposition,缺失则 undefined)。 */
|
||||
export async function downloadBlob(
|
||||
url: string,
|
||||
params?: Record<string, unknown>,
|
||||
): Promise<string | undefined> {
|
||||
const res = await http.get(url, { params, responseType: 'blob' })
|
||||
const blob = res as unknown as Blob
|
||||
// 从响应头取文件名(拦截器只解包 body,需用 raw 拦截;此处 res 已是 blob,header 丢失 → 用固定名)
|
||||
const filename = 'chat-export.html'
|
||||
const objUrl = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = objUrl
|
||||
a.download = filename
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a)
|
||||
URL.revokeObjectURL(objUrl)
|
||||
return filename
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user