添加项目文件。
This commit is contained in:
@ -0,0 +1,333 @@
|
||||
using Jaina;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using HT.Cloud.Code;
|
||||
using HT.Cloud.Domain.FileManage;
|
||||
using HT.Cloud.Service.Event;
|
||||
using HT.Cloud.Service.FileManage;
|
||||
using HT.Cloud.Service.SystemOrganize;
|
||||
|
||||
namespace HT.Cloud.Web.Areas.FileManage.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 创 建:超级管理员
|
||||
/// 日 期:2020-07-22 12:04
|
||||
/// 描 述:文件管理控制器类
|
||||
/// </summary>
|
||||
[Area("FileManage")]
|
||||
public class UploadfileController : BaseController
|
||||
{
|
||||
public UploadfileService _service { get; set; }
|
||||
public SystemSetService _setService { get; set; }
|
||||
|
||||
#region 获取数据
|
||||
|
||||
[HandlerAjaxOnly]
|
||||
[IgnoreAntiforgeryToken]
|
||||
public async Task<ActionResult> GetGridJson(SoulPage<UploadfileEntity> pagination, string keyword)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pagination.field))
|
||||
{
|
||||
pagination.field = "F_Id";
|
||||
pagination.order = "desc";
|
||||
}
|
||||
var data = await _service.GetLookList(pagination, keyword);
|
||||
return Content(pagination.setData(data).ToJson());
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[HandlerAjaxOnly]
|
||||
public async Task<ActionResult> GetListJson(string keyword)
|
||||
{
|
||||
var data = await _service.GetList(keyword);
|
||||
return Content(data.ToJson());
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[HandlerAjaxOnly]
|
||||
public async Task<ActionResult> GetFormJson(string keyValue)
|
||||
{
|
||||
var data = await _service.GetLookForm(keyValue);
|
||||
return Content(data.ToJson());
|
||||
}
|
||||
|
||||
#endregion 获取数据
|
||||
|
||||
#region 提交数据
|
||||
|
||||
[HttpPost]
|
||||
[HandlerLogin]
|
||||
[IgnoreAntiforgeryToken]
|
||||
public async Task<ActionResult> Upload(string fileby, int filetype = 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
//1图片,2excel,3template
|
||||
int[] filetypes = { 1, 2,3 };
|
||||
if (!filetypes.Contains(filetype))
|
||||
{
|
||||
throw new Exception("请指定文件格式");
|
||||
}
|
||||
string stemp = "local";
|
||||
if (_service.currentuser.DbNumber != GlobalContext.SystemConfig.MainDbNumber)
|
||||
{
|
||||
var temp = await _setService.GetForm(_service.currentuser.CompanyId);
|
||||
if (temp != null)
|
||||
{
|
||||
stemp = temp.F_CompanyName;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("租户不存在");
|
||||
}
|
||||
}
|
||||
var files = HttpContext.Request.Form.Files;
|
||||
long size = files.Sum(f => f.Length);
|
||||
if (size > 104857600)
|
||||
{
|
||||
throw new Exception("大小必须小于100M");
|
||||
}
|
||||
List<object> list = new List<object>();
|
||||
foreach (var file in files)
|
||||
{
|
||||
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
|
||||
var ispic = FileHelper.IsPicture(fileName);
|
||||
if (filetype == 1 && !ispic)
|
||||
{
|
||||
throw new Exception("请上传图片");
|
||||
}
|
||||
var isexcle = FileHelper.IsExcel(fileName);
|
||||
if (filetype == 2 && !isexcle)
|
||||
{
|
||||
throw new Exception("请上传Excel");
|
||||
}
|
||||
if (ispic)
|
||||
{
|
||||
filetype = 1;
|
||||
}
|
||||
if (isexcle)
|
||||
{
|
||||
filetype = 2;
|
||||
}
|
||||
string fileValue = "";
|
||||
if (fileby == "公司logo")
|
||||
{
|
||||
fileValue = "icon";
|
||||
}
|
||||
else
|
||||
{
|
||||
fileValue = "file";
|
||||
}
|
||||
string filePath = "";
|
||||
fileName = Utils.CreateNo() + fileName.Substring(fileName.LastIndexOf("."));
|
||||
UploadfileEntity entity = new UploadfileEntity();
|
||||
if (!string.IsNullOrEmpty(stemp))
|
||||
{
|
||||
entity.F_FilePath = $@"/" + fileValue + $@"/" + stemp + $@"/" + DateTime.Now.ToString("yyyyMMdd") + $@"/" + fileName;
|
||||
filePath = GlobalContext.HostingEnvironment.WebRootPath + $@"/" + fileValue + $@"/" + stemp + $@"/" + DateTime.Now.ToString("yyyyMMdd") + $@"/";
|
||||
}
|
||||
string fileFullName = filePath + fileName;
|
||||
entity.Create();
|
||||
entity.F_EnabledMark = true;
|
||||
entity.F_FileBy = fileby;
|
||||
entity.F_FileType = filetype;
|
||||
entity.F_CreatorUserName = _service.currentuser.UserName;
|
||||
entity.F_FileSize = size.ToIntOrNull();
|
||||
|
||||
entity.F_FileName = fileName;
|
||||
entity.F_OrganizeId = _service.currentuser.OrganizeId;
|
||||
if (fileName.LastIndexOf(".") >= 0)
|
||||
{
|
||||
entity.F_FileExtension = fileName.Substring(fileName.LastIndexOf("."));
|
||||
}
|
||||
if (!await SubmitForm(entity, ""))
|
||||
{
|
||||
throw new Exception("数据库操作失败");
|
||||
}
|
||||
if (!Directory.Exists(filePath))
|
||||
{
|
||||
Directory.CreateDirectory(filePath);
|
||||
}
|
||||
using (FileStream fs = System.IO.File.Create(fileFullName))
|
||||
{
|
||||
file.CopyTo(fs);
|
||||
fs.Flush();
|
||||
}
|
||||
list.Add(new { src = entity.F_FilePath, title = fileName });
|
||||
}
|
||||
var log = await _logService.CreateLog("操作成功。", "", "", DbLogType.Visit);
|
||||
await GlobalContext.GetService<IEventPublisher>().PublishAsync(new BaseEventSource("Log:create", log,_service.currentuser));
|
||||
return Content(new { code = 0, msg = "操作成功", data = list }.ToJson());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var log = await _logService.CreateLog(ex.Message, "", "", DbLogType.Visit, true);
|
||||
await GlobalContext.GetService<IEventPublisher>().PublishAsync(new BaseEventSource("Log:create", log, _service.currentuser));
|
||||
return Content(new { code = 400, msg = "操作失败," + ex.Message }.ToJson());
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[HandlerLogin]
|
||||
[IgnoreAntiforgeryToken]
|
||||
public async Task<ActionResult> UploadICE(int filetype = 3)
|
||||
{
|
||||
try
|
||||
{
|
||||
string stemp = "local";
|
||||
if (_service.currentuser.DbNumber != GlobalContext.SystemConfig.MainDbNumber)
|
||||
{
|
||||
var temp = await _setService.GetForm(_service.currentuser.CompanyId);
|
||||
if (temp != null)
|
||||
{
|
||||
stemp = temp.F_CompanyName;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("租户不存在");
|
||||
}
|
||||
}
|
||||
var files = HttpContext.Request.Form.Files;
|
||||
long size = files.Sum(f => f.Length);
|
||||
if (size > 104857600)
|
||||
{
|
||||
throw new Exception("大小必须小于100M");
|
||||
}
|
||||
List<object> list = new List<object>();
|
||||
foreach (var file in files)
|
||||
{
|
||||
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
|
||||
var ispic = FileHelper.IsPicture(fileName);
|
||||
if (filetype == 1 && !ispic)
|
||||
{
|
||||
throw new Exception("请上传图片");
|
||||
}
|
||||
var isexcle = FileHelper.IsExcel(fileName);
|
||||
if (filetype == 2 && !isexcle)
|
||||
{
|
||||
throw new Exception("请上传Excel");
|
||||
}
|
||||
if (ispic)
|
||||
{
|
||||
filetype = 1;
|
||||
}
|
||||
if (isexcle)
|
||||
{
|
||||
filetype = 2;
|
||||
}
|
||||
string fileValue = "file";
|
||||
string filePath = "";
|
||||
fileName = Utils.CreateNo() + fileName.Substring(fileName.LastIndexOf("."));
|
||||
UploadfileEntity entity = new UploadfileEntity();
|
||||
if (!string.IsNullOrEmpty(stemp))
|
||||
{
|
||||
entity.F_FilePath = $@"/" + fileValue + $@"/" + stemp + $@"/" + DateTime.Now.ToString("yyyyMMdd") + $@"/" + fileName;
|
||||
filePath = GlobalContext.HostingEnvironment.WebRootPath + $@"/" + fileValue + $@"/" + stemp + $@"/" + DateTime.Now.ToString("yyyyMMdd") + $@"/";
|
||||
}
|
||||
string fileFullName = filePath + fileName;
|
||||
entity.Create();
|
||||
entity.F_EnabledMark = true;
|
||||
entity.F_FileBy = "ICE富文本";
|
||||
entity.F_FileType = filetype;
|
||||
entity.F_CreatorUserName = _service.currentuser.UserName;
|
||||
entity.F_FileSize = size.ToIntOrNull();
|
||||
|
||||
entity.F_FileName = fileName;
|
||||
entity.F_OrganizeId = _service.currentuser.OrganizeId;
|
||||
if (fileName.LastIndexOf(".") >= 0)
|
||||
{
|
||||
entity.F_FileExtension = fileName.Substring(fileName.LastIndexOf("."));
|
||||
}
|
||||
if (!await SubmitForm(entity, ""))
|
||||
{
|
||||
throw new Exception("数据库操作失败");
|
||||
}
|
||||
if (!Directory.Exists(filePath))
|
||||
{
|
||||
Directory.CreateDirectory(filePath);
|
||||
}
|
||||
using (FileStream fs = System.IO.File.Create(fileFullName))
|
||||
{
|
||||
file.CopyTo(fs);
|
||||
fs.Flush();
|
||||
}
|
||||
list.Add(new { url = entity.F_FilePath, name = fileName,error = 0 });
|
||||
}
|
||||
var log = await _logService.CreateLog("操作成功。", "", "", DbLogType.Visit);
|
||||
await GlobalContext.GetService<IEventPublisher>().PublishAsync(new BaseEventSource("Log:create", log, _service.currentuser));
|
||||
return Content(list.ToJson());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var log = await _logService.CreateLog(ex.Message, "", "", DbLogType.Visit, true);
|
||||
await GlobalContext.GetService<IEventPublisher>().PublishAsync(new BaseEventSource("Log:create", log, _service.currentuser));
|
||||
return Content(new { error = 1 }.ToJson());
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[HandlerLogin]
|
||||
[HandlerAuthorize]
|
||||
public async Task<ActionResult> Download(string keyValue)
|
||||
{
|
||||
var data = await _service.GetForm(keyValue);
|
||||
string filePath = GlobalContext.HostingEnvironment.WebRootPath + $@"/" + data.F_FilePath;
|
||||
if (!FileHelper.IsExistFile(filePath))
|
||||
{
|
||||
return Error("文件不存在");
|
||||
}
|
||||
///定义并实例化一个内存流,以存放图片的字节数组。
|
||||
MemoryStream ms = new MemoryStream();
|
||||
///图片读入FileStream
|
||||
FileStream f = new FileStream(filePath, FileMode.Open);
|
||||
///把FileStream写入MemoryStream
|
||||
ms.SetLength(f.Length);
|
||||
f.Read(ms.GetBuffer(), 0, (int)f.Length);
|
||||
ms.Flush();
|
||||
f.Close();
|
||||
string filename = DateTime.Now.ToString("yyyyMMdd_HHmmss") + data.F_FileExtension;
|
||||
var contentType = MimeMapping.GetMimeMapping(filename);
|
||||
return File(ms, contentType, filename);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[HandlerAjaxOnly]
|
||||
public async Task<bool> SubmitForm(UploadfileEntity entity, string keyValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _service.SubmitForm(entity, keyValue);
|
||||
await Success("操作成功。", "", keyValue);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Error(ex.Message, "", keyValue);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[HandlerAjaxOnly]
|
||||
[HandlerAuthorize]
|
||||
public async Task<ActionResult> DeleteForm(string keyValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _service.DeleteForm(keyValue);
|
||||
return await Success("操作成功。", "", keyValue, DbLogType.Delete);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return await Error(ex.Message, "", keyValue, DbLogType.Delete);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 提交数据
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
@{
|
||||
ViewBag.Title = "Details";
|
||||
Layout = "~/Views/Shared/_Form.cshtml";
|
||||
}
|
||||
<style>
|
||||
.layui-upload-img {
|
||||
width: 350px;
|
||||
height: 350px;
|
||||
margin: 0 10px 10px 0;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
layui.use(['jquery', 'form', 'laydate', 'optimizeSelectOption', 'common'], function () {
|
||||
var form = layui.form,
|
||||
$ = layui.$,
|
||||
common = layui.common,
|
||||
laydate = layui.laydate;
|
||||
var keyValue = $.request("keyValue");
|
||||
//权限字段
|
||||
common.authorizeFields('adminform');
|
||||
$(function () {
|
||||
if (!!keyValue) {
|
||||
common.ajax({
|
||||
url: "/FileManage/Uploadfile/GetFormJson",
|
||||
dataType: "json",
|
||||
data: { keyValue: keyValue },
|
||||
async: false,
|
||||
success: function (data) {
|
||||
common.val('adminform', data);
|
||||
if (data.F_FileType!=1) {
|
||||
common.modalMsg("文件不是图片,无法查看", "warning");
|
||||
}
|
||||
else {
|
||||
$('#demo1').attr('src', data.F_FilePath); //图片链接
|
||||
}
|
||||
common.setReadOnly('adminform');
|
||||
}
|
||||
});
|
||||
}
|
||||
form.render();
|
||||
});
|
||||
wcLoading.close();
|
||||
});
|
||||
</script>
|
||||
<body>
|
||||
<div class="layuimini-container">
|
||||
<div class="layuimini-main">
|
||||
<div class="layui-form layuimini-form">
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">预览图片:</label>
|
||||
<div class="layui-input-block">
|
||||
<img class="layui-upload-img" id="demo1">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
160
HT.Cloud.Web/Areas/FileManage/Views/Uploadfile/Index.cshtml
Normal file
160
HT.Cloud.Web/Areas/FileManage/Views/Uploadfile/Index.cshtml
Normal file
@ -0,0 +1,160 @@
|
||||
@{
|
||||
ViewBag.Title = "Index";
|
||||
Layout = "~/Views/Shared/_Index.cshtml";
|
||||
}
|
||||
<div class="layuimini-container">
|
||||
<div class="layuimini-main">
|
||||
<fieldset class="table-search-fieldset layui-hide" id="searchField">
|
||||
<div>
|
||||
<form class="layui-form layui-form-pane">
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-inline">
|
||||
<label class="layui-form-label">关键字:</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" id="txt_keyword" name="txt_keyword" autocomplete="off" class="layui-input">
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
<button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</fieldset>
|
||||
<script type="text/html" id="toolbarDemo">
|
||||
<div class="layui-btn-container" id="toolbar">
|
||||
<button id="NF-download" name="NF-download" authorize="yes" class="layui-btn layui-btn-sm layui-hide" lay-event="download"><i class="layui-icon"></i>下载</button>
|
||||
<button id="NF-details" name="NF-details" authorize class="layui-btn layui-btn-sm layui-btn-normal layui-hide" lay-event="details"> <i class="layui-icon"></i>查看</button>
|
||||
</div>
|
||||
</script>
|
||||
<script type="text/html" id="currentTableBar">
|
||||
<a id="NF-download" authorize class="layui-btn layui-btn-xs" lay-event="download">下载</a>
|
||||
<a id="NF-details" authorize class="layui-btn layui-btn-xs layui-btn-normal" lay-event="details">查看</a>
|
||||
</script>
|
||||
<table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
layui.use(['jquery', 'form', 'table', 'common','commonTable'], function () {
|
||||
var form = layui.form,
|
||||
table = layui.table,
|
||||
commonTable = layui.commonTable,
|
||||
common = layui.common;
|
||||
//权限控制(js是值传递)
|
||||
currentTableBar.innerHTML = common.authorizeButtonNew(currentTableBar.innerHTML);
|
||||
toolbarDemo.innerHTML = common.authorizeButtonNew(toolbarDemo.innerHTML);
|
||||
commonTable.rendertable({
|
||||
elem: '#currentTableId',
|
||||
id: 'currentTableId',
|
||||
url: '/FileManage/Uploadfile/GetGridJson',
|
||||
filter: {
|
||||
clearFilter: false,
|
||||
//数据量大,data就不需要了
|
||||
items: ['column', 'condition', 'editCondition', 'excel']
|
||||
},
|
||||
cols: [[
|
||||
{ type: "radio", width: 50, fixed: 'left' },
|
||||
{ field: 'F_FilePath', title: '文件路径', width: 120, sort: true, hide: true, filter: true },
|
||||
{ field: 'F_FileName', title: '文件名称', width: 180, sort: true, filter: true },
|
||||
{
|
||||
field: 'F_FileType', title: '文件类型', width: 120, sort: true, filter: true,
|
||||
templet: function (d) {
|
||||
if (d.F_FileType == 1) {
|
||||
return "图片";
|
||||
} else if (d.F_FileType == 2) {
|
||||
return "excel";
|
||||
}
|
||||
else
|
||||
return "其他";
|
||||
}
|
||||
},
|
||||
{ field: 'F_FileSize', title: '文件大小', width: 100, sort: true, hide: true, filter: true },
|
||||
{ field: 'F_FileExtension', title: '文件扩展名', width: 120, sort: true, hide: true, filter: true },
|
||||
{ field: 'F_FileBy', title: '文件所属', width: 120, sort: true, filter: true },
|
||||
{
|
||||
field: 'F_OrganizeName', title: '所属部门', width: 120
|
||||
},
|
||||
{
|
||||
field: 'F_EnabledMark', title: '状态', width: 80, filter: true,
|
||||
templet: function (d) {
|
||||
if (d.F_EnabledMark == true) {
|
||||
return "<span class='layui-btn layui-btn-normal layui-btn-xs'>有效</span>";
|
||||
} else {
|
||||
return "<span class='layui-btn layui-btn-warm layui-btn-xs'>无效</span>";
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
field: 'F_CreatorTime', title: '创建时间', width: 160, sort: true, filter: { type: 'date[yyyy/MM/dd HH:mm:ss]' }
|
||||
},
|
||||
{ field: 'F_CreatorUserName', title: '创建用户', width: 120, sort: true, filter: true },
|
||||
{ field: 'F_Description', title: '文件备注', minWidth: 150, sort: true, filter: true },
|
||||
{ title: '操作', width: 120, toolbar: '#currentTableBar', align: "center", fixed: 'right' }
|
||||
]]
|
||||
});
|
||||
// 监听搜索操作
|
||||
form.on('submit(data-search-btn)', function (data) {
|
||||
//执行搜索重载
|
||||
commonTable.reloadtable({
|
||||
elem: 'currentTableId',
|
||||
curr: 1,
|
||||
where: { keyword: data.field.txt_keyword}
|
||||
});
|
||||
return false;
|
||||
});
|
||||
wcLoading.close();
|
||||
//行点击事件监听,控制按钮显示
|
||||
var oneList = ["NF-download", "NF-details"];//选择1条显示
|
||||
commonTable.tableRowClick("radio", "currentTableFilter", "currentTableId", oneList);
|
||||
//toolbar监听事件
|
||||
table.on('toolbar(currentTableFilter)', function (obj) {
|
||||
var data = table.checkStatus('currentTableId').data;
|
||||
if (obj.event === 'download') {
|
||||
if (data.length == 0) {
|
||||
common.modalMsg("未选中数据", "warning");
|
||||
return false;
|
||||
}
|
||||
window.open('/FileManage/Uploadfile/Download?keyValue=' + data[0].F_Id);
|
||||
}
|
||||
else if (obj.event === 'details') {
|
||||
if (data.length == 0) {
|
||||
common.modalMsg("未选中数据", "warning");
|
||||
return false;
|
||||
}
|
||||
common.modalOpen({
|
||||
title: "查看文件",
|
||||
url: "/FileManage/Uploadfile/Details?keyValue=" + data[0].F_Id,
|
||||
width: "500px",
|
||||
height: "500px",
|
||||
btn: []
|
||||
});
|
||||
}
|
||||
else if (obj.event === 'TABLE_SEARCH') {
|
||||
var _that = $("#searchField");
|
||||
if (_that.hasClass("layui-hide")) {
|
||||
_that.removeClass('layui-hide');
|
||||
} else {
|
||||
_that.addClass('layui-hide');
|
||||
}
|
||||
table.resize();
|
||||
}
|
||||
return false;
|
||||
});
|
||||
//toolrow监听事件
|
||||
table.on('tool(currentTableFilter)', function (obj) {
|
||||
if (obj.event === 'download') {
|
||||
window.open('/FileManage/Uploadfile/Download?keyValue=' + obj.data.F_Id);
|
||||
}
|
||||
else if (obj.event === 'details') {
|
||||
common.modalOpen({
|
||||
title: "查看文件",
|
||||
url: "/FileManage/Uploadfile/Details?keyValue=" + obj.data.F_Id,
|
||||
width: "500px",
|
||||
height: "500px",
|
||||
btn: []
|
||||
});
|
||||
}
|
||||
return false;
|
||||
});
|
||||
});
|
||||
</script>
|
Reference in New Issue
Block a user