From 5292b99af6b7ddad59c26a74ad85b70106b5b200 Mon Sep 17 00:00:00 2001 From: kham vilaythong Date: Sun, 20 Sep 2026 15:30:22 +1000 Subject: [PATCH] fixed download if no image return empty --- .../Controllers/FileUploadController.cs | 17 ++++++--- API/FamilyTreeAPI/Interface/IPersonPhoto.cs | 4 +-- .../Repository/PersonPhotoRepository.cs | 36 ++++++++++++++++++- .../Repository/PersonRepository.cs | 13 ++++--- UI/src/app/attachphoto/photolist.ts | 4 +-- 5 files changed, 60 insertions(+), 14 deletions(-) diff --git a/API/FamilyTreeAPI/Controllers/FileUploadController.cs b/API/FamilyTreeAPI/Controllers/FileUploadController.cs index 7c098fd..2dfd47e 100644 --- a/API/FamilyTreeAPI/Controllers/FileUploadController.cs +++ b/API/FamilyTreeAPI/Controllers/FileUploadController.cs @@ -111,12 +111,21 @@ public class FileUploadController : ControllerBase return Ok(ret); } - [HttpPost("[action]")] - public async Task DownloadPersonPhoto(DownloadFileCriteria criteria) + public async Task DownloadPersonPhotoById(DeleteCriteria criteria) { - var result = await _personPhoto.DownloadPersonPhoto(criteria.Id,criteria.PhotoType ,criteria.FileName); + var result = await _personPhoto.DownloadPersonPhotoById(criteria.Id); + if (result.StatusCode > 0) + return File(result.Data.Content, result.Data.ContentType, result.Data.FileName); + else + return NotFound("not found image"); + } + [HttpPost("[action]")] + public async Task DownloadPersonPhotoPerson(DownloadFileCriteria criteria) + { + + var result = await _personPhoto.DownloadPersonPhotoPerson(criteria.Id,criteria.PhotoType ,criteria.FileName); if (result.StatusCode > 0) return File(result.Data.Content, result.Data.ContentType, result.Data.FileName); else @@ -126,7 +135,7 @@ public class FileUploadController : ControllerBase public async Task DownloadPersonPhoto64(DownloadFileCriteria criteria) { - var result = await _personPhoto.DownloadPersonPhoto(criteria.Id,criteria.PhotoType ,criteria.FileName); + var result = await _personPhoto.DownloadPersonPhotoPerson(criteria.Id,criteria.PhotoType ,criteria.FileName); if (result.StatusCode > 0) { byte[] file64 = result.Data.Content; diff --git a/API/FamilyTreeAPI/Interface/IPersonPhoto.cs b/API/FamilyTreeAPI/Interface/IPersonPhoto.cs index b73a6cf..eba0a4c 100644 --- a/API/FamilyTreeAPI/Interface/IPersonPhoto.cs +++ b/API/FamilyTreeAPI/Interface/IPersonPhoto.cs @@ -5,8 +5,8 @@ namespace FamilyTreeAPI.Interface; public interface IPersonPhoto { Task> SaveAsync(UploadCriteria criteria); - Task> DownloadPersonPhoto(int id, int photoType, string filename); - + Task> DownloadPersonPhotoPerson(int personId, int photoType, string filename); + Task> DownloadPersonPhotoById(int id); Task> DeletePersonPhoto(int id, int photoType); Task> DeletePersonPhotoById(int id); Task>> LoadPersonPhoto(int personId); diff --git a/API/FamilyTreeAPI/Repository/PersonPhotoRepository.cs b/API/FamilyTreeAPI/Repository/PersonPhotoRepository.cs index b1c917a..ab9168c 100644 --- a/API/FamilyTreeAPI/Repository/PersonPhotoRepository.cs +++ b/API/FamilyTreeAPI/Repository/PersonPhotoRepository.cs @@ -93,8 +93,42 @@ namespace FamilyTreeAPI.Repository Message = error }; } + public async Task> DownloadPersonPhotoById(int Id) + { - public async Task> DownloadPersonPhoto(int personId, int photoType, string filename) + int statusCode = -1; + string error = ""; + string extension = ""; + PersonPhoto? model = null; + FileContent fileContent = new(); + try { + + model = await _context.PersonPhotos.FindAsync(Id); + + + if (model != null) + { + extension = Helpers.GetFileExtension(model.Photo); + fileContent.ContentType = Helpers.GetFileContent(extension); + fileContent.Content = (byte[])model.ImagePhoto; + fileContent.FileName = model.Photo; + statusCode = 1; + } + } + catch (Exception e) + { + error = e.ToString(); + statusCode = -1; + } + return new ResultModel() + { + Data = fileContent, + StatusCode = statusCode, + Message = error + }; + } + + public async Task> DownloadPersonPhotoPerson(int personId, int photoType, string filename) { int statusCode = -1; diff --git a/API/FamilyTreeAPI/Repository/PersonRepository.cs b/API/FamilyTreeAPI/Repository/PersonRepository.cs index 6523b92..332f4de 100644 --- a/API/FamilyTreeAPI/Repository/PersonRepository.cs +++ b/API/FamilyTreeAPI/Repository/PersonRepository.cs @@ -756,12 +756,15 @@ public partial class PersonRepository : IPerson // DownloadFileCriteria criteria = new() { FileName = filename }; - var resultModel = await _personPhoto.DownloadPersonPhoto(personId, photoType , filename); - //if (resultModel.Data.Content) - string base64String = Convert.ToBase64String(resultModel.Data.Content); + var resultModel = await _personPhoto.DownloadPersonPhotoPerson(personId, photoType , filename); + if (resultModel.StatusCode > 0) + { - if (!string.IsNullOrEmpty(base64String)) - result = "data:image/png;base64," + base64String; + string base64String = Convert.ToBase64String(resultModel.Data.Content); + + if (!string.IsNullOrEmpty(base64String)) + result = "data:image/png;base64," + base64String; + } return result; } diff --git a/UI/src/app/attachphoto/photolist.ts b/UI/src/app/attachphoto/photolist.ts index 28d66fb..de8cbe6 100644 --- a/UI/src/app/attachphoto/photolist.ts +++ b/UI/src/app/attachphoto/photolist.ts @@ -71,9 +71,9 @@ export class PhotoList implements OnInit, OnDestroy { let typeofCall = "personId_" + id.toString(); - let criteria:any ={id, fileName:''}; + let criteria: any = { id }; this.downloadFile.set(false); - this.http.getFileResponse("api/FileUpload/downloadPersonPhoto", criteria).pipe( + this.http.getFileResponse("api/FileUpload/DownloadPersonPhotoById", criteria).pipe( catchError(err => { console.error(err.message); return EMPTY;