@page "/fileupload1" @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 extensionname = "default"; private string base64data = ""; //you can set a defaut image private string isdisplayimage; //InputFile Chnage event private async Task LoadFiles(InputFileChangeEventArgs e) { isLoading = true; loadedFiles.Clear(); foreach (var file in e.GetMultipleFiles(maxAllowedFiles)) { try { loadedFiles.Add(file); //get the upload file extension. extensionname = Path.GetExtension(file.Name); var imagefiletypes = new List() { ".png",".jpg",".jpeg" }; if(imagefiletypes.Contains(extensionname)){ //resize the image and create the thumbnails var resizedFile = await file.RequestImageFileAsync(file.ContentType, 640, 480); // resize the image file var buf = new byte[resizedFile.Size]; // allocate a buffer to fill with the file's data using (var stream = resizedFile.OpenReadStream()) { await stream.ReadAsync(buf); // copy the stream to the buffer } base64data = "data:image/png;base64," + Convert.ToBase64String(buf); // convert to a base64 string!! //then you can send the base64 data to the server side and insert it into database. //show the thumbnails image isdisplayimage = "block"; } else{ isdisplayimage = "none"; }; } catch (Exception ex) { Logger.LogError("File: {Filename} Error: {Error}", file.Name, ex.Message); } } isLoading = false; } }