Commit 6be37453 by Seniorious

人员照片上传接口修复

parent eb776ebd
...@@ -43,57 +43,41 @@ namespace APIs.Controllers ...@@ -43,57 +43,41 @@ 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 (Image image = Image.FromStream(memoryStream))
{
//图片格式
string extension = string.Empty;
ImageFormat imageFormat = image.RawFormat;
if (imageFormat.Equals(ImageFormat.Jpeg)) using (MemoryStream ms = new MemoryStream())
{
extension = "jpg";
}
else if (imageFormat.Equals(ImageFormat.Png))
{
extension = "png";
}
else if (imageFormat.Equals(ImageFormat.Gif))
{
extension = "gif";
}
if (string.IsNullOrEmpty(extension))
{ {
extension = "jpg"; //拷贝文件到内存流
} req.picture.CopyTo(ms);
var tmp = ms.GetBuffer();
var fileType = tmp[0].ToString() + tmp[1].ToString();
filePath = $"Pictures/{req.policeId}.jpg"; // 图片保存路径 var path = "Pictures";
image.Save(filePath); var fileName = $"{req.policeId}{Path.GetExtension(req.picture.FileName)}";
}
} Directory.CreateDirectory(path);
var filePath = Path.Combine(path, fileName);
Image.FromStream(ms).Save(filePath);
//更新警员照片地址 //更新警员照片地址
police.picUrl = filePath; police.picUrl = filePath;
...@@ -104,15 +88,16 @@ namespace APIs.Controllers ...@@ -104,15 +88,16 @@ namespace APIs.Controllers
{ {
code = ResultCode.OPERATE_SUCCESS.Code, code = ResultCode.OPERATE_SUCCESS.Code,
msg = ResultCode.OPERATE_SUCCESS.Msg, msg = ResultCode.OPERATE_SUCCESS.Msg,
data = "" data = filePath
} : new ApiResult } : new ApiResult
{ {
code = ResultCode.OPERATE_FAILED.Code, code = ResultCode.OPERATE_FAILED.Code,
msg = ResultCode.OPERATE_FAILED.Msg, msg = "更新照片失败"
data = ""
}; };
return src; 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论