FileEdit component

Customized, cross-browser consistent, file input control that supports single file and multiple files upload.

Examples

Single file (default)

On single file mode, when file is selected or user does not cancel Browse dialog, Changed event will be raised. The return value will be a FileChangedEventArgs that will contain only one item in the Files property.
<FileEdit Changed="@OnChanged" />
@code{
    Task OnChanged( FileChangedEventArgs e )
    {
        return Task.CompletedTask;
    }
}

Multiple files

Multiple file uploading is supported by enabling Multiple attribute to component. In this case Files property of FileChangedEventArgs can contain multiple files.
<FileEdit Changed="@OnChanged" Multiple="true" />
@code{
    Task OnChanged( FileChangedEventArgs e )
    {
        return Task.CompletedTask;
    }
}

Limiting to certain file types

You can limit the file types by setting the Filter attribute to a string containing the allowed file type(s). To specify more than one type, separate the values with a comma.

To accept any file type, leave Filter as null (default).

<!-- Accept all image formats by IANA media type wildcard-->
<FileEdit Filter="image/*" />

<!-- Accept specific image formats by IANA type -->
<FileEdit Filter="image/jpeg, image/png, image/gif" />

<!-- Accept specific image formats by extension -->
<FileEdit Filter=".jpg, .png, .gif" />

Events

Changed

This is the main event that will be called every time a user selects a single or multiple files. Depending on the mode in which the FileEdit currently operates. In all cases the event argument is the same. Only difference is that Files array will contain single or multiple items.

Written

This event will be called on every buffer of data that has being written to the destination stream. It is directly related to the MaxMessageSize attribute found on FileEdit component and will contain the information about currently processed file, it’s offset and data array.

Progressed

Similar to the Written, this event will also be called while file is writing to the destination stream but it will contain only the progress and percentage on how much the file is being uploaded.

Started

This event will be called each time one of the selected file(s) has started the upload process.

Ended

This event is fired after the file has ended the upload process. If there was no error it will have Success property set to true.

Examples

WriteToStreamAsync

In this example you can see the usage of all events, including the Written and Progressed. For your own use case you can just focus on Changed event.
@using System.IO

<FileEdit Changed="@OnChanged" Written="@OnWritten" Progressed="@OnProgressed" />
@code{
    string fileContent;

    async Task OnChanged( FileChangedEventArgs e )
    {
        try
        {
            foreach ( var file in e.Files )
            {
                // A stream is going to be the destination stream we're writing to.
                using ( var stream = new MemoryStream() )
                {
                    // Here we're telling the FileEdit where to write the upload result
                    await file.WriteToStreamAsync( stream );

                    // Once we reach this line it means the file is fully uploaded.
                    // In this case we're going to offset to the beginning of file
                    // so we can read it.
                    stream.Seek( 0, SeekOrigin.Begin );

                    // Use the stream reader to read the content of uploaded file,
                    // in this case we can assume it is a textual file.
                    using ( var reader = new StreamReader( stream ) )
                    {
                        fileContent = await reader.ReadToEndAsync();
                    }
                }
            }
        }
        catch ( Exception exc )
        {
            Console.WriteLine( exc.Message );
        }
        finally
        {
            this.StateHasChanged();
        }
    }

    void OnWritten( FileWrittenEventArgs e )
    {
        Console.WriteLine( $"File: {e.File.Name} Position: {e.Position} Data: {Convert.ToBase64String( e.Data )}" );
    }

    void OnProgressed( FileProgressedEventArgs e )
    {
        Console.WriteLine( $"File: {e.File.Name} Progress: {e.Percentage}" );
    }
}

OpenReadStream

Using OpenReadStream on the file you can process the file as it is streamed from the browser to your code, this is mirrors the API found in ASP.NET Core Input File component, for example
@using System.IO

<FileEdit Changed="@OnChanged" Written="@OnWritten" Progressed="@OnProgressed" />
@code{
    const int OneMb = 1024 * 1024;

    async Task OnChanged( FileChangedEventArgs e )
    {
        try
        {
            var file = e.Files.FirstOrDefault();
            if ( file == null )
            {
                return;
            }

            var buffer = new byte[OneMb];
            using ( var bufferedStream = new BufferedStream( file.OpenReadStream( long.MaxValue ), OneMb ) )
            {
                int readCount = 0;
                int readBytes;
                while ( ( readBytes = await bufferedStream.ReadAsync( buffer, 0, OneMb ) ) > 0 )
                {
                    Console.WriteLine( $"Read:{readCount++} {readBytes / (double)OneMb} MB" );
                    // Do work on the first 1MB of data
                }
            }
        }
        catch ( Exception exc )
        {
            Console.WriteLine( exc.Message );
        }
        finally
        {
            this.StateHasChanged();
        }
    }

    void OnWritten( FileWrittenEventArgs e )
    {
        Console.WriteLine( $"File: {e.File.Name} Position: {e.Position} Data: {Convert.ToBase64String( e.Data )}" );
    }

    void OnProgressed( FileProgressedEventArgs e )
    {
        Console.WriteLine( $"File: {e.File.Name} Progress: {e.Percentage}" );
    }
}

Reset

By default after each file upload has finished, file input will automatically reset to it’s initial state. If you want this behavior disabled and control it manually you need to first set AutoReset to false. After that you can call Reset() every time you want the file input to be reset.
<FileEdit @ref="@fileEdit" AutoReset="false" Changed="@OnChanged" />
@code{
    FileEdit fileEdit;

    Task OnChanged( FileChangedEventArgs e )
    {
        return Task.CompletedTask;
    }

    Task OnSomeButtonClick()
    {
        return fileEdit.Reset().AsTask();
    }
}

Attributes

Name Description Type Default
AutoReset If true file input will be automatically reset after it has being uploaded. bool true
BrowseButtonLocalizer Function used to handle browse button localization that will override a default ITextLocalizer. TextLocalizerHandler null
Changed Occurs every time the file(s) has changed. EventCallback<FileChangedEventArgs>
Ended Occurs when an individual file upload has ended. EventCallback<FileEndedEventArgs>
Filter Types of files that the input accepts. string null
MaxMessageSize Max message size (in bytes) when uploading the file. int 20480
Multiple Specifies that multiple files can be selected. bool false
Placeholder Sets the placeholder for the empty file input. string null
Progressed Notifies the progress of file being uploaded. EventCallback<FileProgressedEventArgs>
Started Occurs when an individual file upload has started. EventCallback<FileStartedEventArgs>
Written Occurs every time the part of file has being uploaded. EventCallback<FileWrittenEventArgs>