下载PDF文件
下载PDF文件
这个功能主要还是后端去做,前端不太好控制。
下载的两种类型:
后端返回一个文件地址,然后你通过 window.open或者 模拟 a标签点击都可以,这种方式如果只是打开PDF没问题,但是要是强制下载就可能会跨域。
第二个是后端返回一个流,然后前端接收并保存文件。这种方式如果有问题一定要先通过接口平台测试,确定后端返回数据的正确性。然后再逐步调试,以下是一个接收下载文件流的案例。
function exportToPDF(record: any) {
record.detailLoading = true
exportStudentAssignmentPdfApi({ assignment_id: props.assignmentId, student_uuid: record.uuid })
.then((res) => {
if (res.code && res.code === 0) {
return
}
const blob = new Blob([res], { type: 'application/pdf' })
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = `${assignmentInfo.title}-${record.user_name}.pdf`
link.click()
URL.revokeObjectURL(link.href)
})
.finally(() => {
record.detailLoading = false
})
}