Commit 6be37453 by Seniorious

人员照片上传接口修复

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