I'm trying to read from an existing Excel file that the user will upload via FileSelect. When it gets to the last line trying to set the workbook variable:
workbook = formatProvider.Import(selectedFile.Stream, TimeSpan.FromSeconds(30));
...I get the error, "'System.NotImplementedException' in Telerik.Zip.dll: 'Synchronous actions on the file stream is not supported by the Blazor framework in Blazor Server-side apps due to the SignalR communication between the client and the host. Use the 'ReadAsync' method instead.'"
If I try to copy the stream first like this:
await selectedFile.Stream.CopyToAsync(ms);
It never gets past that line and there is no error.
Here is my stripped code:
@page "/uploadtest"
@using Microsoft.EntityFrameworkCore
@using IGPOSCore.Models
@using Microsoft.IdentityModel.Tokens
@using Telerik.Blazor.Components
@using Telerik.Blazor.Components.FileSelect
@using System.ComponentModel
@using Telerik.Windows.Documents.Spreadsheet.FormatProviders
@using Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx
@using Telerik.Windows.Documents.Spreadsheet.Model
<TelerikFileSelect @ref="fileSelect" OnSelect="@OnFileSelected" Multiple=false />
<TelerikButton OnClick="@(() => LoadExcelFile())" >
Upload
</TelerikButton>
@code {
private TelerikFileSelect? fileSelect { get; set; }
private FileSelectFileInfo? selectedFile { get; set; }
private Workbook? workbook { get; set; }
private async Task OnFileSelected(FileSelectEventArgs args)
{
if (args.Files != null && args.Files.Any())
{
selectedFile = args.Files.First();
}
}
public async Task LoadExcelFile()
{
Guid submissionId = Guid.NewGuid();
if (selectedFile != null)
{
Console.WriteLine($"Processing file: {selectedFile.Name} - {selectedFile.Size} bytes");
IWorkbookFormatProvider formatProvider = new XlsxFormatProvider();
@* var ms = new MemoryStream();
await selectedFile.Stream.CopyToAsync(ms);
workbook = formatProvider.Import(ms, TimeSpan.FromSeconds(30)); *@
workbook = formatProvider.Import(selectedFile.Stream, TimeSpan.FromSeconds(30));
}
}
}
It works if I use a local file instead of the file selected from the FileSelect component.
using (Stream input = new FileStream("test.xlsx", FileMode.Open)) { workbook = formatProvider.Import(input, TimeSpan.FromSeconds(30)); }