Shane Bartholomeusz

Geek and lover of all things tech related

Solved: BlazorInputFile ‘No File Chosen’ Error

Blazor WebAssembly Icon

Problem

Blazor is an exciting new technology for .NET developers that enables us to leverage our core strengths by writing most of the code in C# and minimising the need to write JavaScript code.

While working on a Blazor WebAssembly solution that used Steve Sanderson’s BlazorInputFile solution I came across an unusual issue where the “No File Chosen” text would not change and the selected filename did not appear even though a file had already been selected.

Hmmmm … what’s going on here?

HTML File Input Screenshot

Solution

After a bit of head scratching I decided to look more closely at the BlazorInputFile source code and eventually isolated the issue down to the following inputfile.js JavaScript code.

componentInstance.invokeMethodAsync('NotifyChange', fileList).then(function () {
    //reset file value ,otherwise, the same filename will not be trigger change event again
    elem.value = '';
}, function (err) {
    //reset file value ,otherwise, the same filename will not be trigger change event again
    elem.value = '';
    throw new Error(err);
});

I ended up applying the following code fix to the inputfile.js file in my local solution. Viola! the issue did not occur anymore after retesting it across multiple web browsers including Firefox, Chrome and Edge Chromium, it worked as expected.

componentInstance.invokeMethodAsync('NotifyChange', fileList).then(function () {
    //reset file value ,otherwise, the same filename will not be trigger change event again
    elem.value = elem.value + '_';
}, function (err) {
    //reset file value ,otherwise, the same filename will not be trigger change event again
    elem.value = elem.value + '_';
    throw new Error(err);
});

Final Thoughts

Well I hope the above fix has worked for you. If not, feel free to share any alternative solutions you have found in the comments below to help others out there.

Happy blazor-ing 🙂

Shane Bartholomeusz

1 Comment

  1. Thank you!!!

Leave a Reply

© 2024 Shane Bartholomeusz

Theme by Anders NorenUp ↑