I have a simple Blazor Server app that works ok.
I'm trying to create an equivalent app using Blazor WebAssembly with dotnet 5.
In the working app, I have a razor component that enables the user to upload files using the BlazorFileUploader nuget package
Markup:
<InputFile multiple OnChange="HandleFileSelected"/>
Code behind:
public async Task HandleFileSelected(IFileListEntry[] files)
{
if (!ValidateFiles(files, out _errorMsg))
{
return;
}
etc...
When I try to compile the wasm app, I get the following error
CS1503 Argument 2: cannot convert from 'method group' to 'EventCallback'
Any ideas what I'm doing wrong?
Jonas is correct; your method needs to accept InputFileChangeEventArgs
instead of IFileListEntry[]
. If you hover over OnChange
in Visual Studio 2019, you should see something like this:
The event handler expects a delegate EventCallback
that can accept up to 1
parameter of type InputFileChangeEventArgs
. Think of a delegate
as a method interface: your declaration of IFileListEntry[]
does not match the interface, so you are getting an error.
If you inspect InputFileChangeEventArgs
, you can see how to get the files:
public async Task HandleFileSelected(InputFileChangeEventArgs args)
{
IReadOnlyList<Microsoft.AspNetCore.Components.Forms.IBrowserFile> files =
args.GetMultipleFiles(args.FileCount);
// ...
I dont think the OnChange
eventcallback supports a parameter of type IFileListEntry[]
Try to change your code behind to:
public async Task HandleFileSelected(InputFileChangeEventArgs e)
{
var files = e.GetMultipleFiles();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With