157 lines
5.4 KiB
C#
157 lines
5.4 KiB
C#
using Competition.Common.Util;
|
|
using CompetitionAPI.Util;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using NPOI.HPSF;
|
|
|
|
namespace CompetitionAPI.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class UploadFileController : BaseHandlerController
|
|
{
|
|
Competition.Mysql.BLL.uplaod_file bll = new Competition.Mysql.BLL.uplaod_file();
|
|
|
|
private readonly IWebHostEnvironment _env;
|
|
|
|
public static readonly string Dir = "UploadFile";
|
|
public UploadFileController(IHttpContextAccessor IHttpContextAccessor, IWebHostEnvironment env, IConfiguration iconfiguration)
|
|
{
|
|
Configuration = iconfiguration;
|
|
_env = env;
|
|
context = IHttpContextAccessor.HttpContext!;
|
|
CrossDomain();
|
|
}
|
|
|
|
public bool saveFile(string Dir, IFormFile file, string fileId, out string server_path)
|
|
{
|
|
server_path = "";
|
|
if (null == file)
|
|
{
|
|
return false;
|
|
}
|
|
var directory = Path.Combine(_env.WebRootPath, Dir);
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
var extension = file.FileName.Substring(file.FileName.LastIndexOf("."));
|
|
string NewfileName = fileId + extension;
|
|
|
|
//path = Path.Combine(directory, file.FileName);
|
|
string path = Path.Combine(directory, NewfileName);
|
|
using (var stream = new FileStream(path, FileMode.Create))
|
|
{
|
|
file.CopyTo(stream);
|
|
}
|
|
server_path = "~/" + Dir + "/" + NewfileName;
|
|
return true;
|
|
}
|
|
/// <summary>
|
|
/// 上传更新图片
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
//[Authorize]
|
|
[HttpPost, DisableRequestSizeLimit]
|
|
[Route("upload_image")]
|
|
public JsonResult upload_image()
|
|
{
|
|
try
|
|
{
|
|
JsonResult ret;
|
|
bool state= false;
|
|
var operation_type = GetValue("operation_type");
|
|
if(operation_type== "upload")
|
|
{
|
|
var image_name = GetValue("image_name");
|
|
if (string.IsNullOrEmpty(image_name))
|
|
{
|
|
ret = GetResult(false, "image_name不能为空");
|
|
return ret;
|
|
}
|
|
var model = new Competition.Mysql.Model.uplaod_file();
|
|
model.FileId = Tool.GetId();
|
|
model.FileName = image_name;
|
|
var file_business_photo = GetFile("image_file");
|
|
if (saveFile(Dir, file_business_photo, Tool.GetNextId(model.FileId, 1), out string path))
|
|
{
|
|
model.FilePath = path;
|
|
}
|
|
if (bll.Add(model))
|
|
{
|
|
state= true;
|
|
}
|
|
else
|
|
{
|
|
state= false;
|
|
}
|
|
|
|
}
|
|
else if (operation_type == "update")
|
|
{
|
|
var image_url= GetValue("image_url");
|
|
var list = bll.GetModelList(" FilePath='" + image_url + "'").FirstOrDefault();
|
|
if(list != null)
|
|
{
|
|
var image_name = GetValue("image_name");
|
|
if (string.IsNullOrEmpty(image_name))
|
|
{
|
|
ret = GetResult(false, "image_name不能为空");
|
|
return ret;
|
|
}
|
|
list.FileName = image_name;
|
|
var file_business_photo = GetFile("image_file");
|
|
if (saveFile(Dir, file_business_photo, Tool.GetNextId(list.FileId, 1), out string path))
|
|
{
|
|
list.FilePath = path;
|
|
}
|
|
if (bll.Update(list))
|
|
{
|
|
state = true;
|
|
}
|
|
else
|
|
{
|
|
state = false;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
if (state)
|
|
{
|
|
return GetResult(true, "成功");
|
|
}
|
|
else
|
|
{
|
|
return GetResult(false, "失败");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
|
|
return GetResult(false, "发生错误,请联系管理员");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取所有图片
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("get_images")]
|
|
public JsonResult get_images()
|
|
{
|
|
try
|
|
{
|
|
var list = bll.GetModelList("");
|
|
var result = GetResult(true, list);
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
|
|
return GetResult(false, "发生错误,请联系管理员");
|
|
}
|
|
}
|
|
}
|
|
}
|