diff --git a/BlazorLearnWebApp/Components/Components/UploadAvatar.razor b/BlazorLearnWebApp/Components/Components/UploadAvatar.razor index bc257d6..fdca881 100644 --- a/BlazorLearnWebApp/Components/Components/UploadAvatar.razor +++ b/BlazorLearnWebApp/Components/Components/UploadAvatar.razor @@ -14,6 +14,7 @@ public UserEntity? UserEntity { get; set; } private string? _uploadPath; + private UploadFile? _arg; private async Task OnAvatarUpload(UploadFile arg) { @@ -44,6 +45,7 @@ await arg.SaveToFileAsync(Path.Combine(WebHostEnvironment.WebRootPath, _uploadPath)); arg.PrevUrl = _uploadPath; + _arg = arg;//save the arg } public async Task OnClose(DialogResult result) @@ -57,6 +59,14 @@ UserEntity!.AvatarPath = _uploadPath; await UserEntity.SaveAsync(); } + else + { + if (!string.IsNullOrEmpty(_uploadPath) && _arg != null) + { + //delete new but cancelled avatar + File.Delete(Path.Combine(WebHostEnvironment.WebRootPath, _uploadPath)); + } + } } -} \ No newline at end of file +}