using Common; using Microsoft.AspNetCore.Mvc; namespace CustomerApi.Controllers { [Route("customerapi/[controller]")] [ApiController] public class ImageUploadController : ControllerBase { public class ImageUploadResponse { public string Path { get; set; } = ""; } /// /// 上传图片 - 客服聊天中的图片上传 /// [HttpPost("upload")] public async Task> UploadFile(IFormFile file) { if (file == null || file.Length == 0) { return new BaseResponse { Code = CommonResponseCodeEnum.ParamError, Info = "No file uploaded." }; } var guid = Guid.NewGuid().ToString("N"); var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/uploads", guid + Path.GetExtension(file.FileName)); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } return new BaseResponse { Code = CommonResponseCodeEnum.Success, Info = "上传成功", Data = new ImageUploadResponse { Path = $"uploads/{guid + Path.GetExtension(file.FileName)}" } }; } } }