@page "/file-upload-1" @using System @using System.IO @using Microsoft.AspNetCore.Hosting @using Microsoft.Extensions.Logging @inject ILogger Logger @inject IWebHostEnvironment Environment

Upload Files

@if (isLoading) {

Uploading...

} else { } @code { private List loadedFiles = new(); private long maxFileSize = 1024 * 15; private int maxAllowedFiles = 3; private bool isLoading; private string Imagepath; private async Task LoadFiles(InputFileChangeEventArgs e) { isLoading = true; loadedFiles.Clear(); foreach (var file in e.GetMultipleFiles(maxAllowedFiles)) { try { loadedFiles.Add(file); var path = Path.Combine(Environment.WebRootPath, "Images", file.Name); ////save file to folder //await using FileStream fs = new(path, FileMode.Create); //await file.OpenReadStream(maxFileSize).CopyToAsync(fs); byte[] fileContent = new byte[file.Size]; var result = await file.OpenReadStream(file.Size).ReadAsync(fileContent,0, (Int32)file.Size); // save the fileContent into database. //query the database and get the byte arrary //convert the byte arrary to base64 string, and display it in image element. Imagepath = "data:image/png;base64, " + Convert.ToBase64String(fileContent); } catch (Exception ex) { Logger.LogError("File: {Filename} Error: {Error}", file.Name, ex.Message); } } isLoading = false; } }