Limiting file type to txt with BlazorInputFile

106 views Asked by At

I am using BlazorInputFile and I am having trouble figuring out how to filter my file type to just accept txt files

async Task HandleSelection(BlazorInputFile.IFileListEntry[] files)
{
    try
    {
        var file = files.FirstOrDefault();
        if (file != null)
        {
            if (file.Type != "application/text")
            {
                PopupModel = new PopupModel
                {
                    Message = "Please select .txt file.",
                    MessageType = MessageType.Info,
                    Show = true
                };
                return;
            }
1

There are 1 answers

1
Bennyboy1973 On

You can filter it client side like you would with an HTML file input.

<InputFile accept=".txt" OnChange="HandleFileUpload"></InputFile>

Caution: your idea of what's an acceptable file and Chrome or Firefox's is not guaranteed to be the same. Depending on what you're doing with the file, you might want to check it programmatically-- for example, if your text file contains query strings or html markup or something.