Search code examples
asp.net-coreblazor

OnChange is not working in InputFile Tag in Blazor


OnChange is not working in InputFile Tag in balzor web assembly. LoadFiles is not getting called when I uploaded a file. InputFileChangeEventArgs is not working and breakpoint is not getting hit when a file is uploaded. I have used blazor web app to create this and want to upload an excel which will then show data into the page.

@page "/weather"
@attribute [StreamRendering]

@using System
@using System.IO
@using Microsoft.AspNetCore.Hosting
@inject IWebHostEnvironment Environment

<PageTitle>File Upload 1</PageTitle>

<h1>File Upload Example 1</h1>

<p>
    <label>
        Max file size:
        <input type="number" @bind="maxFileSize" />
    </label>
</p>

<p>
    <label>
        Max allowed files:
        <input type="number" @bind="maxAllowedFiles" />
    </label>
</p>

<p>
    <label>
        Upload up to @maxAllowedFiles of up to @maxFileSize bytes:
        <InputFile OnChange="LoadFiles" multiple />
    </label>
</p>

@if (isLoading)
{
    <p>Uploading...</p>
}
else
{
    <ul>
        @foreach (var file in loadedFiles)
        {
            <li>
                <ul>
                    <li>Name: @file.Name</li>
                    <li>Last modified: @file.LastModified.ToString()</li>
                    <li>Size (bytes): @file.Size</li>
                    <li>Content type: @file.ContentType</li>
                </ul>
            </li>
        }
    </ul>
}

@code {
    private List<IBrowserFile> loadedFiles = new();
    private long maxFileSize = 1024 * 15;
    private int maxAllowedFiles = 3;
    private bool isLoading;

    private async Task LoadFiles(InputFileChangeEventArgs e)
    {
        isLoading = true;
        loadedFiles.Clear();

        foreach (var file in e.GetMultipleFiles(maxAllowedFiles))
        {
            try
            {
                var trustedFileName = Path.GetRandomFileName();
                var path = Path.Combine(Environment.ContentRootPath,
                    Environment.EnvironmentName, "unsafe_uploads",
                    trustedFileName);

                await using FileStream fs = new(path, FileMode.Create);
                await file.OpenReadStream(maxFileSize).CopyToAsync(fs);

                loadedFiles.Add(file);

            }
            catch (Exception ex)
            {
            }
        }

        isLoading = false;
    }
}

Solution

  • You need to use interactive server instead of ssr and stream rendering.

    @rendermode InteractiveServer
    

    This is not wasm. You can also use wasm, but in that case you have to move the component to client project.