2026-10-12 16:22:04 +08:00
|
|
|
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; } = "";
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 00:44:00 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 上传图片 - 客服聊天中的图片上传
|
|
|
|
|
/// </summary>
|
2026-10-12 16:22:04 +08:00
|
|
|
[HttpPost("upload")]
|
|
|
|
|
public async Task<BaseResponse<ImageUploadResponse>> UploadFile(IFormFile file)
|
|
|
|
|
{
|
|
|
|
|
if (file == null || file.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return new BaseResponse<ImageUploadResponse>
|
|
|
|
|
{
|
|
|
|
|
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<ImageUploadResponse>
|
|
|
|
|
{
|
|
|
|
|
Code = CommonResponseCodeEnum.Success,
|
|
|
|
|
Info = "上传成功",
|
|
|
|
|
Data = new ImageUploadResponse { Path = $"uploads/{guid + Path.GetExtension(file.FileName)}" }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|