|
@@ -12,6 +12,7 @@ export const ContentType = {
|
|
|
audio: 'audio/mpeg',
|
|
|
form: 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
|
download: 'application/octet-stream', // for download
|
|
|
+ downloadDocument: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // for download
|
|
|
downloadZip: 'application/zip', // for download
|
|
|
upload: 'multipart/form-data', // for upload
|
|
|
}
|
|
@@ -138,6 +139,7 @@ async function base<T>(url: string, options: FetchOptionType = {}, otherOptions:
|
|
|
needAllResponseContent,
|
|
|
deleteContentType,
|
|
|
getAbortController,
|
|
|
+ fileName,
|
|
|
} = otherOptions
|
|
|
|
|
|
const base
|
|
@@ -195,9 +197,35 @@ async function base<T>(url: string, options: FetchOptionType = {}, otherOptions:
|
|
|
const contentType = res.headers.get('content-type')
|
|
|
if (
|
|
|
contentType
|
|
|
- && [ContentType.download, ContentType.audio, ContentType.downloadZip].includes(contentType)
|
|
|
- )
|
|
|
+ && [ContentType.download, ContentType.audio, ContentType.downloadZip, ContentType.downloadDocument].includes(contentType)
|
|
|
+ ) {
|
|
|
+ if (fileName) {
|
|
|
+ let filename
|
|
|
+ // 尝试从Content-Disposition获取文件名
|
|
|
+ const contentDisposition = res.headers.get('content-disposition')
|
|
|
+ console.log(contentDisposition)
|
|
|
+ if (contentDisposition) {
|
|
|
+ const fileNameMatch = contentDisposition.match(/filename="?(.+)"?/)
|
|
|
+ if (fileNameMatch && fileNameMatch[1])
|
|
|
+ filename = fileNameMatch[1]
|
|
|
+ }
|
|
|
+ const blob = await res.blob()
|
|
|
+ // 创建下载链接
|
|
|
+ const downloadUrl = window.URL.createObjectURL(blob)
|
|
|
+ const a = document.createElement('a')
|
|
|
+ a.href = downloadUrl
|
|
|
+ a.download = fileName || filename || 'download'
|
|
|
+ document.body.appendChild(a)
|
|
|
+ a.click()
|
|
|
+ // 清理
|
|
|
+ setTimeout(() => {
|
|
|
+ document.body.removeChild(a)
|
|
|
+ window.URL.revokeObjectURL(downloadUrl)
|
|
|
+ }, 100)
|
|
|
+ return blob as T
|
|
|
+ }
|
|
|
return await res.blob() as T
|
|
|
+ }
|
|
|
|
|
|
return await res.json() as T
|
|
|
}
|