using learun.iapplication;
using learun.util;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Threading.Tasks;
namespace EC.Web.Areas.LR_SystemModule.Controllers
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.09.18
/// 描 述:图片控制器
///
[Area("LR_SystemModule")]
public class ImgController : MvcControllerBase
{
private readonly ImgIBLL _imgIBLL;
public ImgController(ImgIBLL imgIBLL)
{
_imgIBLL = imgIBLL;
}
#region 获取数据
///
/// 获取图片文件
///
/// 图片编码
///
[HttpGet]
public IActionResult Down(string id)
{
//ImgEntity imgEntity = null;// await _imgIBLL.GetEntity(id);
//if (imgEntity != null && !string.IsNullOrEmpty(imgEntity.F_Content))
//{
// string imgContent = imgEntity.F_Content.Replace("data:image/" + imgEntity.F_ExName.Replace(".", "") + ";base64,", "");
// byte[] arr = Convert.FromBase64String(imgContent);
// return File(arr, "application/octet-stream");
//}
//else
//{
//}
byte[] arr2;
switch (id)
{
case "logo_default":
arr2 = FileHelper.ReadRoot("/img/logo_default.png");
break;
case "adminDefault":
arr2 = FileHelper.ReadRoot("/img/adminDefault.png");
break;
default:
arr2 = FileHelper.ReadRoot("/img/logo_default.png");
break;
}
return File(arr2, "application/octet-stream");
}
#endregion 获取数据
#region 提交数据
///
/// 上传图片
///
///
[HttpPost]
public IActionResult UploadFile(string code)
{
var files = Request.Form.Files;
//没有文件上传,直接返回
if (files[0].Length == 0 || string.IsNullOrEmpty(files[0].FileName))
{
return Fail("没有文件信息");
}
string FileEextension = Path.GetExtension(files[0].FileName);
ImgEntity imgEntity = new ImgEntity();
imgEntity.F_Name = files[0].FileName;
imgEntity.F_ExName = FileEextension;
byte[] bytes = new byte[files[0].Length];
MemoryStream ms = new MemoryStream();
files[0].CopyTo(ms);
ms.Flush();
ms.Position = 0;
ms.Read(bytes, 0, bytes.Length);
ms.Close();
ms.Dispose();
imgEntity.F_Content = Convert.ToBase64String(bytes);
_imgIBLL.SaveEntity(code, imgEntity);
return SuccessInfo("上传成功。");
}
#endregion 提交数据
}
}