Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>10.0.17</Version>
<Version>10.0.18</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/components/BootstrapBlazor.PdfReader/PdfReader.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
stream = await OnGetStreamAsync();
}

await SetPdfStream(stream);
await InvokeSetDataAsync(stream);
}
}

private async Task SetPdfStream(Stream? stream)
private async Task InvokeSetDataAsync(Stream? stream)
{
if (stream == null || stream == Stream.Null)
{
Expand Down
28 changes: 22 additions & 6 deletions src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function setData(id, data) {

const { options } = pdf;
options.url = null;
options.data = data;;
options.data = data;
await loadPdf(pdf);
}

Expand Down Expand Up @@ -528,12 +528,19 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
});
EventHandler.on(toolbar, "click", ".bb-view-download", e => {
if (options.url) {
let fileName = "download.pdf";
const docTitle = el.querySelector('.bb-view-subject');
const anchorElement = document.createElement('a');
anchorElement.href = options.url;
anchorElement.download = docTitle.textContent;
anchorElement.click();
anchorElement.remove();
if (docTitle) {
fileName = docTitle.textContent;
}
downloadPdf(options.url, fileName);
}
else if (options.data) {
const blob = new Blob([options.data], { type: 'application/pdf' });
const url = window.URL.createObjectURL(blob);
let fileName = "download.pdf";
Comment thread
ArgoZhang marked this conversation as resolved.
downloadPdf(url, fileName);
window.URL.revokeObjectURL(url);
Comment thread
ArgoZhang marked this conversation as resolved.
}
});

Expand Down Expand Up @@ -563,6 +570,15 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
});
}

const downloadPdf = (url, filename) => {
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = filename;
document.body.appendChild(anchorElement);
anchorElement.click();
anchorElement.remove();
}

const removeToolbarEventHandlers = el => {
if (el) {
const towPagesOneView = el.querySelector(".dropdown-item-pages");
Expand Down