Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created January 29, 2024 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/f3e8778f72f2f76a724291722fab237c to your computer and use it in GitHub Desktop.
Save bjoerntx/f3e8778f72f2f76a724291722fab237c to your computer and use it in GitHub Desktop.
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@inject IJSRuntime JsRuntime
@inject NavigationManager Navigator
<script src="@(BasePath)TextControl/GetResource?Resource=minimized.tx-viewer.min.js"></script>
<script src="@(BasePath)TextControl/GetResource?Resource=minimized.tx-viewer-component.min.js"></script>
<tx-document-viewer
id="viewer1"
settings='{"documentData":"VGV4dCBDb250cm9s", "dock":1, "basePath":"@(BasePath?.TrimEnd('/'))"}'>
</tx-document-viewer>
@code
{
[Parameter]
public string? BasePath { get; set; }
private DotNetObjectReference<TXDocumentViewer> DotNetReference => DotNetObjectReference.Create(this);
private IJSObjectReference? _txdocumentviewer;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_txdocumentviewer = await JsRuntime.InvokeAsync<IJSObjectReference>("import", "./scripts/txdocumentviewer.js");
}
}
[JSInvokable("LoadDocumentFromFile")]
public void LoadDocumentFromFile(string filename)
{
// check if the file exists
if (!System.IO.File.Exists(filename))
{
return;
}
// load the file into a byte array
byte[] bDocument = System.IO.File.ReadAllBytes(filename);
// invoke the JS function 'loadDocument' to load back to the modified document
_txdocumentviewer?.InvokeVoidAsync("loadDocument", new object[] { Convert.ToBase64String(bDocument), filename });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment