loading

Inspect selected files

GetFileInfo / GetFileInfos

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).

Razor
<input type="file" @ref="fileInput" multiple />

@code {
    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
        }
    }
}
Live sample
Pick one or more files
file info output
Results will appear here when you interact with the samples.

Read file contents

ReadAsText / ReadAsBytes

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.

C#
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);
Live sample
Pick a file (a small text file works best)
file content output
Results will appear here when you interact with the samples.

Data URL previews

ReadAsDataUrl

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.

C#
string dataUrl = await fileReader.ReadAsDataUrl(fileInput);

// <img src="@imagePreviewUrl" /> in the markup
imagePreviewUrl = dataUrl;
Live sample
Pick an image
data URL output
Results will appear here when you interact with the samples.

Reset the selection

Clear

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).

C#
await fileReader.Clear(fileInput);
Live sample
Pick a file or two, then clear them
clear output
Results will appear here when you interact with the samples.
Note:
Bytes cross the interop boundary ReadAsBytes and ReadAsText marshal the whole file content from JavaScript into .NET memory in one call. That is perfect for documents, images and configuration files; for very large files consider uploading directly from the browser instead of round-tripping the bytes through C#.

API reference

Member
Signature
Description
GetFileInfo
ValueTask<BlobInfo?> GetFileInfo(ElementReference inputElement, int index = 0)
Returns metadata for the file at the given index in the input's files list, or null when none exists.
GetFileInfos
ValueTask<BlobInfo[]> GetFileInfos(ElementReference inputElement)
Returns metadata for every file currently selected in the input.
ReadAsBytes
ValueTask<byte[]?> ReadAsBytes(ElementReference inputElement, int index = 0)
Reads a single file as raw bytes, or null when no file exists at the index.
ReadAsText
ValueTask<string> ReadAsText(ElementReference inputElement, int index = 0, string encoding = "utf-8")
Reads a single file as text using the given encoding (UTF-8 by default).
ReadAsDataUrl
ValueTask<string> ReadAsDataUrl(ElementReference inputElement, int index = 0)
Reads a single file as a base-64 data URL, ready for use as an img source.
Clear
ValueTask Clear(ElementReference inputElement)
Clears the input's selection (resets its files list).
BlobInfo.Name
string Name
For File objects: the original file name. Empty for plain Blobs.
BlobInfo.Type
string Type
MIME type (e.g. image/png). Empty when unknown.
BlobInfo.Size
long Size
Size in bytes.
BlobInfo.LastModified
long LastModified
Last-modified time as Unix epoch milliseconds. 0 for plain Blobs.
An unhandled error has occurred. Reload 🗙