feat(knowledge): 切换下载模板的方式

1. 由创建 a 链接的方式换成 window.open
2. 更改获取 链接的 方式
This commit is contained in:
Huangzhe
2025-04-17 15:37:52 +08:00
parent 3f9f96646d
commit 7240e8d1b6
2 changed files with 4 additions and 34 deletions

View File

@@ -26,9 +26,9 @@ export function getRuleList(data = {}) {
* 下载知识文件模板 * 下载知识文件模板
*/ */
export function downloadKnowledgeTemplate() { export function downloadKnowledgeTemplate() {
return request({ return {
url: getUrl('/datasetDocumentEx/download'), url: getUrl('/datasetDocumentEx/download'),
method: 'get', method: 'get',
responseType: 'blob' responseType: 'blob'
}) }
} }

View File

@@ -114,38 +114,8 @@ export default {
this.$emit('getFileType', this.form.radio) this.$emit('getFileType', this.form.radio)
}, },
downloadTemplate() { downloadTemplate() {
downloadKnowledgeTemplate().then((res) => { console.log(`下载模板的链接是:${downloadKnowledgeTemplate().url}`)
// 创建Blob URL window.open(downloadKnowledgeTemplate().url, '_blank')
const blob = new Blob([res.data])
const url = window.URL.createObjectURL(blob)
console.log(`blob url: `, url)
// 创建一个临时a标签并触发点击
const link = document.createElement('a')
link.href = url
// 尝试从响应头获取文件名,如果没有则使用默认名称
// const contentDisposition = res.headers['content-disposition']
// console.log(`content-disposition: `, contentDisposition)
let filename = '知识文件模板.pdf'
// if (contentDisposition) {
// const filenameMatch = contentDisposition.match(/filename="?(.+)"?/)
// if (filenameMatch && filenameMatch[1]) {
// filename = filenameMatch[1]
// }
// }
link.setAttribute('download', filename)
document.body.appendChild(link)
link.click()
// 清理
window.URL.revokeObjectURL(url)
document.body.removeChild(link)
}).catch(error => {
this.$message.error(error)
})
}, },
// 生成filed // 生成filed