protected virtual async Task OnFileSelectedAsync(ChangeEventArgs args) { EditContext.NotifyPropertyBusy(Property); UploadCompletion = 0.01; var files = await FileReaderService.CreateReference(_fileInput).EnumerateFilesAsync(); if (files.Count() > 1) { EditContext.AddValidationMessage(Property, "This editor only supports single files for now."); } var file = files.FirstOrDefault(); if (file == null) { return; } var fileInfo = await file.ReadFileInfoAsync(); var validationMessages = FileUploadHandler.ValidateFile(fileInfo); if (validationMessages.Any()) { foreach (var message in validationMessages) { EditContext.AddValidationMessage(Property, message); } EditContext.NotifyPropertyFinished(Property); StateHasChanged(); return; } using var uploadedFile = await UploadFileToTempFileAsync(file, 8192, fileInfo.Size, (completion) => { if (completion - UploadCompletion > 1) { UploadCompletion = completion; StateHasChanged(); } }); UploadCompletion = 0.0; StateHasChanged(); var value = await FileUploadHandler.SaveFileAsync(fileInfo, uploadedFile); SetValueFromObject(value); EditContext.NotifyPropertyFinished(Property); EditContext.NotifyPropertyChanged(Property); }
protected virtual async Task OnFileSelectedAsync(ChangeEventArgs args) { EditContext.NotifyPropertyBusy(Property); UploadCompletion = 0.01; var files = await FileReaderService.CreateReference(_fileInput).EnumerateFilesAsync(); if (files.Count() > 1) { EditContext.AddValidationMessage(Property, "This editor only supports single files for now."); } var file = files.FirstOrDefault(); if (file == null) { return; } var fileInfo = await file.ReadFileInfoAsync(); IEnumerable <string> validationMessages; try { validationMessages = await FileUploadHandler.ValidateFileAsync(new FileInfoModel(fileInfo)); } catch { validationMessages = new[] { "Failed to validate file." }; } if (!validationMessages.Any()) { try { using var uploadedFile = await UploadFileToTempFileAsync(file, 8192, fileInfo.Size, (completion) => { Console.WriteLine(completion); if (completion - UploadCompletion > 1) { UploadCompletion = completion; StateHasChanged(); } }); UploadCompletion = 0.0; StateHasChanged(); var value = await FileUploadHandler.SaveFileAsync(new FileInfoModel(fileInfo), uploadedFile); await SetValueFromObjectAsync(value); EditContext.NotifyPropertyFinished(Property); } catch { validationMessages = new[] { "Failed to upload file." }; } } if (validationMessages.Any()) { foreach (var message in validationMessages) { EditContext.AddValidationMessage(Property, message); } EditContext.NotifyPropertyFinished(Property); UploadCompletion = 0.0; StateHasChanged(); return; } }