原生html下载pdf 空白问题解决
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>下载pdf</title>
</head>
<body>
<button id="btn">下载</button>
<script>
function downloadFn() {
let that = this
this.loading = true
var xhr = new XMLHttpRequest() // 用这种原生请求下载后端返回的二进制流打开就不会出现空白
xhr.open('get', './a.pdf')
xhr.responseType = 'blob'
xhr.onload = function () {
that.loading = false
const url = window.URL.createObjectURL(this.response)
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', 'a.pdf')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
xhr.send()
}
document.getElementById('btn').onclick = function () {
downloadFn()
}
</script>
</body>
</html>
本文标题:原生html下载pdf 空白问题解决
本文链接:https://www.haomeiwen.com/subject/nxkzqdtx.html
网友评论