I would like to open a pdf document via a button. Is this possible in Blazor? Thanks for your help
Asked
Active
Viewed 5,473 times
1
-
Blazor runs on the browser. Anything you can do on the browser you can do with Blazor. That includes displaying PDF files using eg the `embed` tag or any of the Javascript viewers. Where is the file stored? On the server? Client? Another web site? – Panagiotis Kanavos Jan 27 '22 at 12:47
-
Does this answer your question? [How to display PDF file in HTML?](https://stackoverflow.com/questions/17784037/how-to-display-pdf-file-in-html) – Panagiotis Kanavos Jan 27 '22 at 12:47
-
1The file is stored on a server. – MaxB Jan 27 '22 at 12:49
-
1I will try, thanks :) – MaxB Jan 27 '22 at 12:49
2 Answers
0
Please try this solution
This is your javascript function
function openFile(data) {
var link = this.document.createElement('a');
link.download = data.fileName;
link.href = data.url;
link.target ="_blank";
this.document.body.appendChild(link);
link.click();
this.document.body.removeChild(link);
}
This is your razor file, replace url with your pdf download url, clicking on button will open pdf in new browser tab
@inject IJSRuntime JS
<button @onclick=@(()=> JS.InvokeVoidAsync("openFile", new {fileName="anyfileName", url="http://anyurl.com"}))>Download PDF</button>

Surinder Singh
- 1,165
- 1
- 3
- 11
0
Use HTML?
Download:
<a href=@PdfURL download>Download</a>
or
Open in new tab:
<a href=@PdfURL target="_blank">Open</a>
or
Open in current tab (i.e. Navigate to it) :
<a href=@PdfURL>Open</a>
?

Mister Magoo
- 7,452
- 1
- 20
- 35