我对blazor非常陌生,我正在制作一个用于处理TDT文件的简单应用程序。我一直在使用Tewr的文件加载器,但是每当尝试上载267 KB的目录时,它都会崩溃,并指出它已用完内存。我究竟做错了什么?预先感谢。
@page "/sheetupload"
@using CurrieTechnologies.Razor.Clipboard
@inject ClipboardService clipboard
@using System.IO;
@inject IFileReaderService fileReaderService;
<h1>Hello, world!</h1>
<input type="file" @ref=fileRef multiple webkitdirectory/>
<button @onclick=ProcessFile>Read file</button>
@if (output != null)
{
<button @onclick="(async e => await clipboard.WriteTextAsync(output))">Copy To Clipboard</button>
<table>
<tbody><pre>@output</pre></tbody>
</table>
}
@code {
ElementReference fileRef;
List<List<object>> sheet;
string output;
async Task ProcessFile()
{
sheet = new List<List<object>>();
var files = (await fileReaderService.CreateReference(fileRef).EnumerateFilesAsync()).ToList();
foreach (IFileReference file in files)
{
using (Stream stream = await file.OpenReadAsync())
{
int size = (int)(await file.ReadFileInfoAsync()).Size;
byte[] buffer = new byte[size];
await stream.ReadAsync(buffer, 0, size);
output = buffer.Length.ToString();
string rawData = System.Text.Encoding.ASCII.GetString(buffer);
List<object> currentRow = new List<object>();
string currentCell = "";
for (int i = 0; i < rawData.Length; i++)
{
if (rawData[i] == '\t')
{
currentRow.Add(currentCell);
}else if (rawData[i] == '\n')
{
currentRow.Add(currentCell);
sheet.Add(currentRow);
}
else
{
currentCell += rawData[i];
}
}
}
}