2025-03-24 03:26:11 +00:00
|
|
|
|
using LY.App.Model;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LY.App.Controllers
|
|
|
|
|
|
{
|
2025-04-02 14:17:11 +00:00
|
|
|
|
[Route("api/[controller]/[action]")]
|
2025-03-24 03:26:11 +00:00
|
|
|
|
public class UploadController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly string uploadPath = "upload";
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("uploadImg")]
|
|
|
|
|
|
public async Task<IActionResult> UploadImg()
|
|
|
|
|
|
{
|
|
|
|
|
|
ApiResult result = new ApiResult();
|
|
|
|
|
|
result.code = 1;
|
|
|
|
|
|
if (!Request.HasFormContentType)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest("未选择文件");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IFormFileCollection cols = Request.Form.Files;
|
|
|
|
|
|
if (cols.Count == 0)
|
|
|
|
|
|
return BadRequest("上传失败");
|
|
|
|
|
|
const string fileFilt = ".gif|.jpg|.jpeg|.png|.bmp";
|
|
|
|
|
|
foreach (IFormFile file in cols)
|
|
|
|
|
|
{
|
|
|
|
|
|
var fileExtension = Path.GetExtension(file.FileName);
|
|
|
|
|
|
|
|
|
|
|
|
if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
result.msg = "上传的文件格式不正确";
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long length = file.Length;
|
|
|
|
|
|
if (length > 1024 * 1024 * 5)
|
|
|
|
|
|
{
|
|
|
|
|
|
result.msg = "上传的文件不能大于5M";
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var strDateTime = DateTime.Now.ToString("yyMMddhhmmssfff");
|
|
|
|
|
|
var strRan = Convert.ToString(new Random().Next(100, 999));
|
|
|
|
|
|
var fileName = strDateTime + strRan + fileExtension;
|
|
|
|
|
|
string path = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "Img"));
|
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
var filePath = Path.Combine(path, fileName);
|
|
|
|
|
|
|
|
|
|
|
|
using (var stream = new FileStream(filePath, FileMode.Create))
|
|
|
|
|
|
{
|
|
|
|
|
|
file.CopyTo(stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
result.data = $"/{uploadPath}/{fileName}";
|
|
|
|
|
|
result.code = 0;
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return BadRequest("上传失败");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|