FileReader
Read the files a user picks in an input element - as metadata, raw bytes, text or data URLs - without touching IJSRuntime or streaming interop yourself.
@inject Bit.Butil.FileReader fileReaderMDN reference
Capture a plain input element as an ElementReference (ref capture) and hand it to FileReader. GetFileInfo returns metadata for one file by index; GetFileInfos returns the whole selection. Metadata arrives as BlobInfo: Name, Type (MIME), Size in bytes and LastModified (Unix epoch milliseconds).
<input type="file" @ref="fileInput" multiple />
{
private ElementReference fileInput;
private async Task Inspect()
{
BlobInfo? first = await fileReader.GetFileInfo(fileInput);
BlobInfo[] all = await fileReader.GetFileInfos(fileInput);
foreach (var info in all)
{
// info.Name, info.Type, info.Size, info.LastModified
}
}
}ReadAsText decodes a file with the given encoding (UTF-8 by default - pass a different one for legacy sources). ReadAsBytes returns the raw byte array, or null when no file exists at the requested index. Both take an optional index into the input's files list.
string text = await fileReader.ReadAsText(fileInput);
// non-UTF-8 sources:
string legacy = await fileReader.ReadAsText(fileInput, index: 0, encoding: "windows-1252");
byte[]? bytes = await fileReader.ReadAsBytes(fileInput);ReadAsDataUrl returns the file as a base-64 data URL (data:image/png;base64,...), which drops straight into an img src attribute for instant previews. Prefer ReadAsBytes when you intend to process the content - base-64 inflates the payload by roughly a third.
string dataUrl = await fileReader.ReadAsDataUrl(fileInput);
// <img src="@imagePreviewUrl" /> in the markup
imagePreviewUrl = dataUrl;Clear resets the input's files list, exactly like the user re-opening the picker and cancelling. Useful after an upload completes or when the same file must be selectable twice in a row (the change event doesn't re-fire for an identical selection).
await fileReader.Clear(fileInput);API reference
ValueTask<BlobInfo?> GetFileInfo(ElementReference inputElement, int index = 0)ValueTask<BlobInfo[]> GetFileInfos(ElementReference inputElement)ValueTask<byte[]?> ReadAsBytes(ElementReference inputElement, int index = 0)ValueTask<string> ReadAsText(ElementReference inputElement, int index = 0, string encoding = "utf-8")ValueTask<string> ReadAsDataUrl(ElementReference inputElement, int index = 0)ValueTask Clear(ElementReference inputElement)string Namestring Typelong Sizelong LastModified