Commit 6be37453 by Seniorious

人员照片上传接口修复

parent eb776ebd
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<ApplicationIcon>平台.ico</ApplicationIcon> <ApplicationIcon>平台.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
......
...@@ -43,76 +43,61 @@ namespace APIs.Controllers ...@@ -43,76 +43,61 @@ namespace APIs.Controllers
/// 人员照片上传 /// 人员照片上传
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpGet] [HttpPost]
public async Task<ApiResult> UploadPicture([FromForm] PictureReq req)
public async Task<ApiResult> UploadPicture([FromBody] PictureReq req)
{ {
try try
{ {
var police = await _policeService.QueryOne(p => p.id == req.policeId); var police = await _policeService.QueryOne(p => p.id == req.policeId);
//警员不存在 //警员不存在
if(police == null) if (police == null)
{ {
var error = new ApiResult var error = new ApiResult
{ {
code = ResultCode.POLICE_ERROR.Code, code = ResultCode.POLICE_ERROR.Code,
msg = ResultCode.POLICE_ERROR.Msg, msg = "警员不存在",
}; };
return error; return error;
} }
//存储图片 //存储图片
byte[] imageBytes = req.picture; var ext = Path.GetExtension(req.picture.FileName).ToLower();
string filePath = "";
using (MemoryStream memoryStream = new MemoryStream(imageBytes)) using (MemoryStream ms = new MemoryStream())
{ {
using (Image image = Image.FromStream(memoryStream)) //拷贝文件到内存流
{ req.picture.CopyTo(ms);
//图片格式 var tmp = ms.GetBuffer();
string extension = string.Empty; var fileType = tmp[0].ToString() + tmp[1].ToString();
ImageFormat imageFormat = image.RawFormat;
if (imageFormat.Equals(ImageFormat.Jpeg)) var path = "Pictures";
{ var fileName = $"{req.policeId}{Path.GetExtension(req.picture.FileName)}";
extension = "jpg";
}
else if (imageFormat.Equals(ImageFormat.Png))
{
extension = "png";
}
else if (imageFormat.Equals(ImageFormat.Gif))
{
extension = "gif";
}
if (string.IsNullOrEmpty(extension))
{
extension = "jpg";
}
filePath = $"Pictures/{req.policeId}.jpg"; // 图片保存路径 Directory.CreateDirectory(path);
image.Save(filePath); var filePath = Path.Combine(path, fileName);
}
}
//更新警员照片地址 Image.FromStream(ms).Save(filePath);
police.picUrl = filePath;
police.updateTime = DateTime.Now;
bool rs = await _policeService.Update(police);
var src = rs ? new ApiResult //更新警员照片地址
{ police.picUrl = filePath;
code = ResultCode.OPERATE_SUCCESS.Code, police.updateTime = DateTime.Now;
msg = ResultCode.OPERATE_SUCCESS.Msg, bool rs = await _policeService.Update(police);
data = ""
} : new ApiResult
{
code = ResultCode.OPERATE_FAILED.Code,
msg = ResultCode.OPERATE_FAILED.Msg,
data = ""
};
return src; var src = rs ? new ApiResult
{
code = ResultCode.OPERATE_SUCCESS.Code,
msg = ResultCode.OPERATE_SUCCESS.Msg,
data = filePath
} : new ApiResult
{
code = ResultCode.OPERATE_FAILED.Code,
msg = "更新照片失败"
};
return src;
}
} }
catch (Exception ex) catch (Exception ex)
{ {
...@@ -124,7 +109,6 @@ namespace APIs.Controllers ...@@ -124,7 +109,6 @@ namespace APIs.Controllers
}; };
return error; return error;
} }
} }
/// <summary> /// <summary>
......
...@@ -3,6 +3,6 @@ ...@@ -3,6 +3,6 @@
public class PictureReq public class PictureReq
{ {
public string policeId { get; set; } public string policeId { get; set; }
public byte[] picture { get; set; } public IFormFile picture { get; set; }
} }
} }
...@@ -17,6 +17,7 @@ using Repositories.Repository.UnitOfWork; ...@@ -17,6 +17,7 @@ using Repositories.Repository.UnitOfWork;
using Common.Utility.RabbitMQ; using Common.Utility.RabbitMQ;
using Common.Global; using Common.Global;
using System.Xml.Linq; using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
namespace APIs namespace APIs
{ {
...@@ -40,10 +41,11 @@ namespace APIs ...@@ -40,10 +41,11 @@ namespace APIs
services.AddControllers() services.AddControllers()
.AddNewtonsoftJson(options => .AddNewtonsoftJson(options =>
{ {
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
}); });
//services.AddControllers(options => { options.Filters.Add(new ConsumesAttribute("multipart/form-data")); });
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer(); services.AddEndpointsApiExplorer();
services.AddAutoMapper(typeof(MapperProfile)); services.AddAutoMapper(typeof(MapperProfile));
......
...@@ -7,9 +7,7 @@ ...@@ -7,9 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="SqlSugar"> <PackageReference Include="SqlSugarCore" Version="5.1.4.106" />
<HintPath>..\..\..\..\..\Users\DELL\.nuget\packages\sqlsugarcore\5.1.4.106\lib\netstandard2.1\SqlSugar.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
</Project> </Project>
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论