we can't able to upload the file in InputFile component when registering custom dependencies injection AutofacServiceProviderFactory. find the below code on Program.cs file. builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
please find the sample code below for your reference.
@using System
@using System.IO
@using Microsoft.AspNetCore.Hosting
@using Microsoft.Extensions.Logging
Upload Files
Max file size:
Max allowed files:
Upload up to @maxAllowedFiles of up to @maxFileSize bytes:
@if (isLoading)
{
Uploading...
}
else
{
@foreach (var file in loadedFiles)
{
Name: @file.Name
Last modified: @file.LastModified.ToString()
Size (bytes): @file.Size
Content type: @file.ContentType
}
}
@code {
private List 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
{
loadedFiles.Add(file);
var trustedFileNameForFileStorage = Path.GetRandomFileName();
var path = @"D:\" + file.Name;
await using FileStream fs = new(path, FileMode.Create);
await file.OpenReadStream(maxFileSize).CopyToAsync(fs);
}
catch (Exception ex)
{
Console.WriteLine("File: {Filename} Error: {Error}",
file.Name, ex.Message);
}
}
isLoading = false;
}
}
Need to upload the file successfully.
set
DisableImplicitFromServicesParametersto true.From here: https://github.com/dotnet/aspnetcore/issues/38842#issuecomment-1342540950