fixed download if no image return empty

This commit is contained in:
2026-09-20 15:30:22 +10:00
parent 3d83e95613
commit 5292b99af6
5 changed files with 60 additions and 14 deletions
@@ -111,12 +111,21 @@ public class FileUploadController : ControllerBase
return Ok(ret);
}
[HttpPost("[action]")]
public async Task<IActionResult> DownloadPersonPhoto(DownloadFileCriteria criteria)
public async Task<IActionResult> DownloadPersonPhotoById(DeleteCriteria<int> 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<IActionResult> 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<IActionResult> 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;
+2 -2
View File
@@ -5,8 +5,8 @@ namespace FamilyTreeAPI.Interface;
public interface IPersonPhoto
{
Task<ResultModel<int>> SaveAsync(UploadCriteria criteria);
Task<ResultModel<FileContent>> DownloadPersonPhoto(int id, int photoType, string filename);
Task<ResultModel<FileContent>> DownloadPersonPhotoPerson(int personId, int photoType, string filename);
Task<ResultModel<FileContent>> DownloadPersonPhotoById(int id);
Task<ResultModel<int>> DeletePersonPhoto(int id, int photoType);
Task<ResultModel<int>> DeletePersonPhotoById(int id);
Task<ResultModel<List<PersonPhotoDto>>> LoadPersonPhoto(int personId);
@@ -93,8 +93,42 @@ namespace FamilyTreeAPI.Repository
Message = error
};
}
public async Task<ResultModel<FileContent>> DownloadPersonPhotoById(int Id)
{
public async Task<ResultModel<FileContent>> 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<FileContent>()
{
Data = fileContent,
StatusCode = statusCode,
Message = error
};
}
public async Task<ResultModel<FileContent>> DownloadPersonPhotoPerson(int personId, int photoType, string filename)
{
int statusCode = -1;
@@ -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)
var resultModel = await _personPhoto.DownloadPersonPhotoPerson(personId, photoType , filename);
if (resultModel.StatusCode > 0)
{
string base64String = Convert.ToBase64String(resultModel.Data.Content);
if (!string.IsNullOrEmpty(base64String))
result = "data:image/png;base64," + base64String;
}
return result;
}
+2 -2
View File
@@ -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;