Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How do I handle empty files in Blazor Server InputFile after cancelling?

$
0
0

I'm coding a Blazor Server Web Application, specifically a form. I'm struggling with an InputSelect field, and I couldn't find any solution to this specific problem.

Here's a basic example I made based on my actual code:

@page "/problem-sample"<EditForm Model="@formModel" OnSubmit="HandleFormSubmit"><InputFile OnChange="HandleFileUpload" /></EditForm><h3>@(fileIsComplete ? "A file is uploaded" : "A file isn't uploaded")</h3>@code {    public class FormModel    {        public IBrowserFile? File { get; set; }    }    private FormModel formModel = new();    private bool fileIsComplete = false;    public async Task HandleFormSubmit()    {        if (formModel.File == null)        {            return;        }    }    private async Task HandleFileUpload(InputFileChangeEventArgs e)    {        var file = e.File;        if (file != null)        {            fileIsComplete = true;            return;        }    }}

The expected behaviour is:

  • If the user doesn't upload any file, fileIsComplete should be false.
  • If the user hits the button and uploads a file, fileIsComplete should be true.
  • If the user hits the button, but presses cancel or closes the window, fileIsComplete should be false, even if the user previously uploaded a file.

Currently, if the user cancels or closes the window after uploading a file, InputFile clears, but fileIsComplete keeps true:

Problem Image Reference

I tried checking if the file is nullable, using expression body or using stateHasChanged directly, but nothing seems to work.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>