![](/img/trans.png)
[英](HTML) Download a PDF file instead of opening them in browser when clicked
[英]Download a PDF link as a file in the same page instead of opening in browser
在我用 Ionic 5 和 Angular 8 編寫的應用程序中,我需要在單擊鏈接時在同一頁面中下載多個 PDF 鏈接(文件)。 我試過了,但 PDF 鏈接正在瀏覽器中打開。 請求幫助我實現此功能。
https://stackblitz.com/edit/ionic-yzzwov?file=pages%2Fhome%2Fhome.html
同源時有下載屬性正常顯示,否則顯示為無download
屬性。 列出三個解決方案:
downloadFileXHR(filePath, fileName) {
var xhh = new XMLHttpRequest()
xhh.open('get', filePath)
xhh.responseType = 'blob'
xhh.onreadystatechange = function () {
if (xhh.readyState === 4 && xhh.status === 200) {
var blob = new Blob([xhh.response])
var csvUrl = URL.createObjectURL(blob)
var link = document.createElement('a')
link.href = csvUrl
link.download = fileName
link.click()
}
}
xhh.send()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.