添加项目文件。
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
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<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)}" }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user