change all save to database no folder anymore

This commit is contained in:
2026-09-19 22:37:43 +10:00
parent ba4976fce4
commit 75b49be476
4 changed files with 68 additions and 19 deletions
@@ -283,7 +283,7 @@ public partial class PersonRepository : IPerson
node.Key = item.Id.ToString();
node.Type = type;
if (item.Image != null)
node.Data.Image = await LoadPersonImage(item.Image);
node.Data.Image = await LoadPersonImage(item.Id,item.Image);
node.Icon = "T";
node.StyleClass = getStyleClass(node.Icon);
@@ -319,8 +319,8 @@ public partial class PersonRepository : IPerson
TreeNode<TreeData> child;
PersonDto dto;
Person pe;
int fatherId, motherId;
fatherId = motherId = 0;
int fatherId, motherId, personId;
personId = fatherId = motherId = 0;
string data = person.Sex;
TreeNode<TreeData> citem;
string pName = "";
@@ -371,6 +371,7 @@ public partial class PersonRepository : IPerson
pName = pe.FirstName;
image = pe.Image;
key = motherId.ToString();
personId = motherId;
data = pe.Sex;
}
}
@@ -382,6 +383,7 @@ public partial class PersonRepository : IPerson
pName = pe.FirstName;
image = pe.Image;
key = fatherId.ToString();
personId = fatherId;
data = pe.Sex;
}
}
@@ -390,7 +392,7 @@ public partial class PersonRepository : IPerson
citem.Data = new();
citem.Label = pName;
if (image != null)
citem.Data.Image = await LoadPersonImage(image);
citem.Data.Image = await LoadPersonImage(personId,image);
citem.Icon = "P";
@@ -411,7 +413,7 @@ public partial class PersonRepository : IPerson
child.Type = type;
image = dto.Image;
if (image != null)
child.Data.Image = await LoadPersonImage(image);
child.Data.Image = await LoadPersonImage(dto.Id,image);
child.Icon = "C";
child.StyleClass = getStyleClass(child.Icon);
child.Label = dto.FirstName;
@@ -480,12 +482,12 @@ public partial class PersonRepository : IPerson
/// <param name="fileName"></param>
/// <param name="familyId"></param>
/// <param name="base64"></param>
private string SaveFile(string fileName, int familyId, string base64)
private async Task<string> SaveFile(string fileName, int personId, string base64)
{
string path = "";
int statusCode = 0;
string error = "";
// string error = "";
string sdate = GetCurrentDateTime();
string filename, extention;
filename = extention = "";
@@ -494,14 +496,33 @@ public partial class PersonRepository : IPerson
string rbase = base64.Substring(first, last - first);
byte[] newBytes = Convert.FromBase64String(rbase);
extention = System.IO.Path.GetExtension(fileName);
filename = personId + "_" + sdate + extention;
// Wrap the byte array in a MemoryStream
var stream = new MemoryStream(newBytes);
// Create the IFormFile instance
IFormFile file = new FormFile(stream, 0, newBytes.Length, "Profile", filename)
{
Headers = new HeaderDictionary(),
ContentType = "application/octet-stream" // Change to your specific MIME type if known (e.g., "image/jpeg", "application/pdf")
};
//save to database.
UploadCriteria criteria = new();
criteria.FileName = filename;
criteria.PersonId = personId;
criteria.PhotoType = (int) enumPhotoType.Profile;
criteria.File = file;
var rval = await _personPhoto.SaveAsync(criteria);
/*
path = _config["AppSettings:ImageFolder"];
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
extention = System.IO.Path.GetExtension(fileName);
filename = familyId + "_" + sdate + extention;
string fullpath = System.IO.Path.Combine(path, filename);
/*
using (var fileStream = new FileStream(fullpath, FileMode.Create))
@@ -520,10 +541,12 @@ public partial class PersonRepository : IPerson
}
*/
/*
using (FileStream fileStream = new FileStream(fullpath, FileMode.Create, FileAccess.Write, FileShare.None))
{
fileStream.Write(newBytes, 0, newBytes.Length);
}
*/
return filename;
}
@@ -538,7 +561,7 @@ public partial class PersonRepository : IPerson
string loginName = "";
if (httpContext != null)
{
UserDto? user = (UserDto?)httpContext.Items["User"];
UserDto? user = (UserDto?)httpContext.Items["User"];
if (user != null)
loginName = user.FirstName + " " + user.LastName;
}
@@ -571,7 +594,7 @@ public partial class PersonRepository : IPerson
if (!string.IsNullOrEmpty(container.FileName))
{
string image = SaveFile(container.FileName, result, container.FormData);
string image = await SaveFile(container.FileName, result, container.FormData);
// update the image in table to new
model.Image = image;
await _context.SaveChangesAsync();
@@ -587,7 +610,7 @@ public partial class PersonRepository : IPerson
result = item.Id;
if (!string.IsNullOrEmpty(container.FileName))
{
string image = SaveFile(container.FileName, result, container.FormData);
string image = await SaveFile(container.FileName, result, container.FormData);
// update the image in table to new
model1.Image = image;
}
@@ -714,17 +737,34 @@ public partial class PersonRepository : IPerson
};
}
private async Task<string> LoadPersonImage(string filename)
private async Task<string> LoadPersonImage_old(string filename)
{
string result = "";
DownloadFileCriteria criteria = new() { FileName = filename };
var resultModel = await _importPersonRepository.DownloadFile(criteria);
if (!string.IsNullOrEmpty((resultModel.Data)))
if (!string.IsNullOrEmpty(resultModel.Data))
result = "data:image/png;base64," + resultModel.Data;
return result;
}
private async Task<string> LoadPersonImage(int personId,string filename)
{
string result = "";
// DownloadFileCriteria criteria = new() { FileName = filename };
var resultModel = await _personPhoto.DownloadPersonPhoto(personId, filename);
//if (resultModel.Data.Content)
string base64String = Convert.ToBase64String(resultModel.Data.Content);
if (!string.IsNullOrEmpty(base64String))
result = "data:image/png;base64," + base64String;
return result;
}
public ResultModel<int> DeleteUploadFile(DeleteFileCriteria criteria)
{
int result = -1;