Skip to content

Commit af88fca

Browse files
committed
refactor: 增加 download 功能
1 parent ff8463f commit af88fca

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,19 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
528528
});
529529
EventHandler.on(toolbar, "click", ".bb-view-download", e => {
530530
if (options.url) {
531+
let fileName = "download.pdf";
531532
const docTitle = el.querySelector('.bb-view-subject');
532-
const anchorElement = document.createElement('a');
533-
anchorElement.href = options.url;
534-
anchorElement.download = docTitle.textContent;
535-
anchorElement.click();
536-
anchorElement.remove();
533+
if (docTitle) {
534+
fileName = docTitle.textContent;
535+
}
536+
downloadPdf(options.url, fileName);
537+
}
538+
else if (options.data) {
539+
const blob = new Blob([options.data], { type: 'application/pdf' });
540+
const url = window.URL.createObjectURL(blob);
541+
let fileName = "download.pdf";
542+
downloadPdf(url, fileName);
543+
window.URL.revokeObjectURL(url);
537544
}
538545
});
539546

@@ -563,6 +570,15 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
563570
});
564571
}
565572

573+
const downloadPdf = (url, filename) => {
574+
const anchorElement = document.createElement('a');
575+
anchorElement.href = url;
576+
anchorElement.download = filename;
577+
document.body.appendChild(anchorElement);
578+
anchorElement.click();
579+
anchorElement.remove();
580+
}
581+
566582
const removeToolbarEventHandlers = el => {
567583
if (el) {
568584
const towPagesOneView = el.querySelector(".dropdown-item-pages");

0 commit comments

Comments
 (0)