question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor InputFile doesn't notice user cancellation

See original GitHub issue

Describe the bug

I have an InputFile component and upload a file with it. The event OnChange gets raised fine. Now I click again on the browse button to change my uploaded document(s) and click on cancel. The component says nothing is selected now but the event doesn’t raise again. So how should I detect that the user has deleted his/her upload?

To Reproduce

Consider the following code which writes the file to an Directory called Uploads.

<InputFile OnChange="OnInputFileChange" class="form-control" accept="application/pdf" />
@code 
{
	private async Task OnInputFileChange(InputFileChangeEventArgs e)
	{
		string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Uploads");
		string filename = Path.Combine(path, e.File.Name);

		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
		}

		byte[] buffer = new byte[e.File.Size];
		await e.File.OpenReadStream().ReadAsync(buffer);

		await File.WriteAllBytesAsync(filename, buffer);
	}
}

Without detection for deletion I am not able to do validation. Consider my model for the input has a string which stores the path to the uploaded file which is being set within this method. So the user clicks on the button again and cancels the operation. Because a cancel i snot recognized, I would still have my string in my model set to the file, the user has uploaded before. In addtion this results in unnesscary files on the server, which could have been easily removed if there was any way to detect user cancellation.

Further technical details

  • ASP.NET Core version 5.0.2
  • VS Studio 2019 - 16.8.4 .NET SDK (gemäß “global.json”): Version: 5.0.200-preview.20601.7 Commit: b3b934bbf2

Laufzeitumgebung: OS Name: Windows OS Version: 10.0.19042 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.200-preview.20601.7\

Host (useful for support): Version: 5.0.2 Commit: cb5f173b96

.NET SDKs installed: 3.1.400-preview-015203 [C:\Program Files\dotnet\sdk] 5.0.100-rc.1.20452.10 [C:\Program Files\dotnet\sdk] 5.0.100 [C:\Program Files\dotnet\sdk] 5.0.102 [C:\Program Files\dotnet\sdk] 5.0.200-preview.20601.7 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.0-rc.1.20451.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.0-rc.1.20451.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.0-rc.1.20452.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
georgehemmingscommented, Nov 3, 2021

The cancellation isn’t detected as a change because Blazor is clearing the input’s value when it’s clicked. By registering your own click handler you can clean up your own state (the path or reference to the IBrowserFile object). I’ve found by adding a @key to the InputFile you can force the UI to create a new input element, which resolved the issue raised in #34040.

https://blazorrepl.telerik.com/mPFPYxvG34GY8AcP03

<div>
    <InputFile @key="uploadedCount" OnChange="@FileChange" @onclick="FileClick" />
</div>

<button @onclick="Upload" disabled="@(selectedFile == null)">Upload</button>

<p>@message</p>

@code {

    private int uploadedCount = 0;
    private string message;

    public void Upload()
    {
        if (selectedFile == null)
            return;

        // Upload selectedFile

        message = $"{selectedFile.Name} uploaded!";

        selectedFile = null;
        uploadedCount++;
    }

    IBrowserFile selectedFile;

    private void FileClick()
    {
        selectedFile = null;
    }

    private void FileChange(InputFileChangeEventArgs e)
    {
        selectedFile = e.File;
    }
}

0reactions
msftbot[bot]commented, Jun 18, 2022

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core Blazor file uploads
A file upload component can detect when a user has cancelled an upload by using a CancellationToken when calling into the IBrowserFile.
Read more >
How to detect when cancel is clicked on file input?
If the user loads, opens the file-dialog, then cancels out, they never see the upload button. If the user chooses a file, the...
Read more >
FileUploadStartEventArgs.Cancel Property | Blazor
Specifies whether the file upload operation should be canceled. ... Use the event argument's FileInfo property to get information about the file (its...
Read more >
Getting a filestream from a blazor component file upload
I'm tasked with creating an upload form using server side blazor. ... Maybe you should use the Blazor built-in InputFile component.
Read more >
HxInputFile – Blazor InputFile extension with direct (native ...
Starting with .NET 5 there is a InputFile component in Blazor which allows you to access content of the corresponding file(s) in form...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found