Get请求文件流下载 从二进制流获取文件名称 封装相关请求方法

相关核心代码

export const doHzExport = (url, params) => {
	return request.get(baseURL + url, {
		params, // 参数会作为查询字符串附加到URL
		responseType: 'blob' // 设置响应类型为blob
	})
}
export function exportTemplate (data) {
	return doHzExport(`qualityAssessment/flowHandle/exportTemplate`, data)
}

const exportFile = async (id) => {
	const res = await exportTemplate({ recordId: id })
	const disposition = res.headers.get('Content-Disposition')
	let fileName = disposition.split('\'\'')[1]
	if (res.status === 200) {
		mitt.emit('refresh-steps')
		let blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
		let link = document.createElement('a')
		link.href = window.URL.createObjectURL(blob)
		link.download = decodeURI(fileName)
		link.click()
	}
}

posted @ 2025-06-27 11:29  Felix_Openmind  阅读(36)  评论(0)    收藏  举报
*{cursor: url(https://files-cdn.cnblogs.com/files/morango/fish-cursor.ico),auto;}