Skip to content

Commit 3f032cc

Browse files
committed
refactor: 重构下载 pdf 功能
1 parent e609ce8 commit 3f032cc

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -526,23 +526,15 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
526526
pdfViewer.spreadMode = 0;
527527
}
528528
});
529-
EventHandler.on(toolbar, "click", ".bb-view-download", e => {
529+
EventHandler.on(toolbar, "click", ".bb-view-download", async e => {
530530
let fileName = el.getAttribute('data-bb-download');
531-
if (options.url) {
532-
if (fileName === null) {
533-
const docTitle = el.querySelector('.bb-view-subject');
534-
if (docTitle) {
535-
fileName = docTitle.textContent;
536-
}
531+
if (fileName === null) {
532+
const docTitle = el.querySelector('.bb-view-subject');
533+
if (docTitle) {
534+
fileName = docTitle.textContent;
537535
}
538-
downloadPdf(options.url, fileName);
539-
}
540-
else if (options.data) {
541-
const blob = new Blob([options.data], { type: 'application/pdf' });
542-
const url = window.URL.createObjectURL(blob);
543-
downloadPdf(url, fileName);
544-
window.URL.revokeObjectURL(url);
545536
}
537+
await downloadPdf(options, fileName);
546538
});
547539

548540
EventHandler.on(toolbar, "click", ".dropdown-item-presentation", async e => {
@@ -571,16 +563,23 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
571563
});
572564
}
573565

574-
const downloadPdf = (url, fileName) => {
566+
const downloadPdf = async (options, fileName) => {
575567
if (fileName === null) {
576568
fileName = "download.pdf";
577569
}
578-
const anchorElement = document.createElement('a');
579-
anchorElement.href = url;
580-
anchorElement.download = fileName;
581-
document.body.appendChild(anchorElement);
582-
anchorElement.click();
583-
document.body.removeChild(anchorElement);
570+
571+
await getPdfUrl(options, url => {
572+
const anchorElement = document.createElement('a');
573+
anchorElement.href = url;
574+
anchorElement.download = fileName;
575+
document.body.appendChild(anchorElement);
576+
anchorElement.click();
577+
document.body.removeChild(anchorElement);
578+
579+
return new Promise((resolve, reject) => {
580+
resolve();
581+
});
582+
});
584583
}
585584

586585
const removeToolbarEventHandlers = el => {

0 commit comments

Comments
 (0)