From 1a02038328eda9e1e18ec1c374f13759ae6c3418 Mon Sep 17 00:00:00 2001 From: "Scott Huang(ZhiLiang)" Date: Mon, 22 Dec 2025 13:46:26 +0800 Subject: [PATCH] Delete new avatar which have upload but cancelled Delete new avatar which have upload but cancelled. Or else, it is a but to allow user uploads any physical avatar but not used, the worse scenario may full use website storage. --- .../Components/Components/UploadAvatar.razor | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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 +}