Browse Source

封装加载图片的方法,在更新数据前先删除原来的图片

develop-FileUpload-test
胡超1 2 years ago
parent
commit
30fd529f75
  1. 75
      24Hour/Controllers/Common/LawyerArchivesController.cs

75
24Hour/Controllers/Common/LawyerArchivesController.cs

@ -1782,6 +1782,25 @@ namespace _24Hour.Controllers.Common
lawyerservices = default(List<App_LawyerServicesModel>), lawyerservices = default(List<App_LawyerServicesModel>),
users = default(List<App_Sys_UserModel>) users = default(List<App_Sys_UserModel>)
}); });
//替换数据前先删除之前保存的图片
var deleteOldData = (string? jsonpath) =>
{
if (string.IsNullOrEmpty(jsonpath))
{
return;
}
var photos = jsonpath.ConvertToModel<List<string>>() ?? new List<string>();
foreach (var photo in photos.Where(x => x != null))
{
var urlpath = photo.Split('/');
var filepath = Path.Combine(Environment.CurrentDirectory, "wwwroot", Path.Combine(urlpath));
if (System.IO.File.Exists(filepath))
{
System.IO.File.Delete(filepath);
}
}
};
foreach (var userim in importdata.users.Where(x => x != null)) foreach (var userim in importdata.users.Where(x => x != null))
{ {
var user = await _db.Queryable<App_Sys_UserModel>() var user = await _db.Queryable<App_Sys_UserModel>()
@ -1818,6 +1837,9 @@ namespace _24Hour.Controllers.Common
} }
else else
{ {
deleteOldData(user.cardIdphoto);
deleteOldData(user.identityphoto);
user.cardIdphoto = cardids; user.cardIdphoto = cardids;
user.identityphoto = files; user.identityphoto = files;
user.unitCode = _userdata.unitCode; user.unitCode = _userdata.unitCode;
@ -1842,46 +1864,23 @@ namespace _24Hour.Controllers.Common
{ {
var svc = await _db.Queryable<App_LawyerServicesModel>() var svc = await _db.Queryable<App_LawyerServicesModel>()
.Where(x => x.Id == lawyersvc.Id).FirstAsync(); .Where(x => x.Id == lawyersvc.Id).FirstAsync();
var loadphoto = (string dirname) =>
var annexphoto = Path.Combine(ziplawyersvcs, lawyersvc.Id, "annexs"); {
var annexs = new DirectoryInfo(annexphoto) var dirpath = Path.Combine(ziplawyersvcs, lawyersvc.Id, dirname);
.GetFiles() var photos = new DirectoryInfo(dirpath)
.Select(x => "/CaseFile/imports/" + Path.GetRelativePath(dir, x.FullName)
.Replace(@"\", @"/"))
.ToList()
.ConvertToJsonStr();
var legalAidPhoto = Path.Combine(ziplawyersvcs, lawyersvc.Id, "legalAidPhoto");
var legalAidPhotos = new DirectoryInfo(legalAidPhoto)
.GetFiles()
.Select(x => "/CaseFile/imports/" + Path.GetRelativePath(dir, x.FullName)
.Replace(@"\", @"/"))
.ToList()
.ConvertToJsonStr();
var relationshipProofPhoto = Path.Combine(ziplawyersvcs, lawyersvc.Id, "relationshipProofPhoto");
var relationshipProofPhotos = new DirectoryInfo(relationshipProofPhoto)
.GetFiles()
.Select(x => "/CaseFile/imports/" + Path.GetRelativePath(dir, x.FullName)
.Replace(@"\", @"/"))
.ToList()
.ConvertToJsonStr();
var otherPhoto = Path.Combine(ziplawyersvcs, lawyersvc.Id, "otherPhoto");
var otherPhotos = new DirectoryInfo(otherPhoto)
.GetFiles() .GetFiles()
.Select(x => "/CaseFile/imports/" + Path.GetRelativePath(dir, x.FullName) .Select(x => "/CaseFile/imports/" + Path.GetRelativePath(dir, x.FullName)
.Replace(@"\", @"/")) .Replace(@"\", @"/"))
.ToList() .ToList()
.ConvertToJsonStr(); .ConvertToJsonStr();
return photos;
};
var departmentPhoto = Path.Combine(ziplawyersvcs, lawyersvc.Id, "departmentPhoto"); var annexs = loadphoto("annexs");
var departmentPhotos = new DirectoryInfo(departmentPhoto) var legalAidPhotos = loadphoto("legalAidPhoto");
.GetFiles() var relationshipProofPhotos = loadphoto("relationshipProofPhoto");
.Select(x => "/CaseFile/imports/" + Path.GetRelativePath(dir, x.FullName) var otherPhotos = loadphoto("otherPhoto");
.Replace(@"\", @"/")) var departmentPhotos = loadphoto("departmentPhoto");
.ToList()
.ConvertToJsonStr();
if (svc == null) if (svc == null)
{ {
@ -1912,6 +1911,14 @@ namespace _24Hour.Controllers.Common
.Where(x => x.identity == "律师") .Where(x => x.identity == "律师")
.FirstAsync(); .FirstAsync();
deleteOldData(svc?.annex);
deleteOldData(svc?.legalAidPhoto);
deleteOldData(svc?.relationshipProofPhoto);
deleteOldData(svc?.otherPhoto);
deleteOldData(svc?.departmentPhoto);
svc.createuserId = lawyer.Id; svc.createuserId = lawyer.Id;
svc.annex = annexs; svc.annex = annexs;
svc.legalAidPhoto = legalAidPhotos; svc.legalAidPhoto = legalAidPhotos;

Loading…
Cancel
Save