using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QMAPP.Common.Web.Controllers;
using QMFrameWork.WebUI.Attribute;
using QMAPP.Web.Models.Sys;
using QMAPP.Entity.Sys;
using QMFrameWork.Data;
using QMFrameWork.WebUI.DataSource;
using System.Configuration;
using System.Web.Script.Serialization;
using System.Data;
using QMAPP.ServicesAgent;
using QMFrameWork.Common.Serialization;
using System.Text.RegularExpressions;
namespace QMAPP.Web.Controllers
{
///
/// 文件管理
/// 创建人:王丹丹
/// 创建日期:2015-03-16
///
public class FileInfoController : QController
{
#region 字符串常量
private const string uploadFolder = "\\FileInfo\\";
#endregion
#region 主页
public ActionResult Main()
{
return View();
}
#endregion
#region 获取列表
///
/// 加载列表
///
/// 结果
[HandleException]
public ActionResult List(bool? callBack)
{
FileInfoModel seachModel = new FileInfoModel();
if (callBack == true)
TryGetSelectBuffer(out seachModel);
string typeNo = Request.Params["TYPENO"];
seachModel.rownumbers = false;
seachModel.url = "/FileInfo/GetList?TYPENO=" + typeNo;
#region 类别列表选择的分类
//类别列表选择的分类
if (!string.IsNullOrEmpty(typeNo))
{
string[] typeNoList = typeNo.Split(',');
ViewData["TYPENO"] = typeNoList[0];
}
#endregion
return View("List", seachModel);
}
///
/// 获取列表
///
/// 是否回调
/// 列表
[HandleException]
public ActionResult GetList(bool? callBack)
{
FileInfoModel seachModel = null;
DataPage page = null;
FileInfo condition = null;
try
{
//获取查询对象
seachModel = GetModel();
#region 获取缓存值
if (callBack != null)
{
TryGetSelectBuffer(out seachModel);
}
else
{
//保存搜索条件
SetSelectBuffer(seachModel);
}
#endregion
//获取前台分页设置信息
page = this.GetDataPage(seachModel);
condition = CopyToModel(seachModel);
#region 类别列表选择的分类
//类别列表选择的分类
string typeNo = Request.Params["TYPENO"];
if (!string.IsNullOrEmpty(typeNo))
{
string[] typeNoList = typeNo.Split(',');
condition.TYPENO = typeNoList[0];
ViewData["TYPENO"] = typeNoList[0];
}
#endregion
#region 调用服务
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
page = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_GetList.ToString(), condition, page);
#endregion
#region 转换回复数量格式
//转换回复数量格式
List list = JsonConvertHelper.GetDeserialize>(page.Result.ToString());
foreach (var item in list)
{
if (item.FILESTORAGENAME != "" && item.FILESTORAGENAME != null)
{
item.FileStorageNameTxt = ChangeFilePath(item.FILESTORAGENAME);
}
}
#endregion
DataGridResult result = new DataGridResult();
result.Total = page.RecordCount;
result.Rows = list;
//result.Rows = JsonConvertHelper.GetDeserialize>(list.ToString());
//result.Rows = JsonConvertHelper.GetDeserialize>(page.Result.ToString());
return Content(result.GetJsonSource());
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 编辑
///
/// 编辑载入
///
/// 处理结果
[HandleException]
public ActionResult Edit()
{
FileInfoModel model = new FileInfoModel();
string ID = Request.Params["PID"];
FileInfo Entity = new FileInfo();
try
{
if (string.IsNullOrEmpty(ID) == false)
{
//修改获取原数据
Entity.PID = ID;
#region 调用服务
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
Entity = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_Get.ToString(), Entity);
#endregion
model = CopyToModel(Entity);
if (!string.IsNullOrEmpty(Entity.FILESTORAGENAME))
{
//获取附件名称
model.ATTACHFILETXT = ChangeFilePath(Entity.FILESTORAGENAME);
}
}
return View(model);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 保存
///
/// 保存
///
///
/// 处理结果
[HttpPost]
[HandleException]
[ValidateInput(false)]
public ActionResult Save(FileInfoModel saveModel, HttpPostedFileBase file)
{
try
{
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
FileInfo Entity = new FileInfo(); ;
int count = 0;//是否保存成功
TryUpdateModel(saveModel);
//获取保存信息
Entity = CopyToModel(saveModel);
FileInfo model = new FileInfo();
//新增
try
{
//获取文件保存路径
string trueFilePath = "";
trueFilePath = FilePath(Entity.FILESTORAGENAME);
if (!string.IsNullOrEmpty(trueFilePath))
{
#region 保存附件
//修改通知
if (!string.IsNullOrEmpty(Entity.PID))
{
#region 修改附件
FileInfo fileinfo = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_Get.ToString(), Entity);
//如果修改了上传附件,校验附件是否存在
if (trueFilePath != fileinfo.FILESTORAGENAME)
{
//判断文件路径是否存在
bool issuccess = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_ExistsFile.ToString(), trueFilePath);
if (issuccess)
{
SetMessage(AppResource.FileNameIsHave);
return View("Edit", saveModel);
}
else
{
//删除原附件
agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_DeleteFile.ToString(), fileinfo.FILESTORAGENAME);
//上传新附件
model = this.UploadFile(file, trueFilePath);
if (model.IsSuccess == false)
{
SetMessage(model.InfoError);
return View("Edit", saveModel);
}
}
}
#endregion
}
//新建通知
else
{
#region 新建附件
//判断文件路径是否存在
bool issuccess = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_ExistsFile.ToString(), trueFilePath);
if (issuccess)
{
SetMessage(AppResource.FileNameIsHave);
return View("Edit", saveModel);
}
else
{
//上传附件
model = this.UploadFile(file, trueFilePath);
if (model.IsSuccess == false)
{
SetMessage(model.InfoError);
return View("Edit", saveModel);
}
}
#endregion
}
#endregion
}
Entity.FILESTORAGENAME = trueFilePath;
if (string.IsNullOrEmpty(Entity.PID) == true)
{
count = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_Insert.ToString(), Entity);
}
else
{
count = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_Update.ToString(), Entity);
}
}
catch (Exception ex)
{
throw ex;
}
if (count == -1)
{
return this.GetJsViewResult(string.Format("parent.List(1);parent.showTitle('{0}');parent.closeAppWindow1();", "保存失败"));
}
else
{
return this.GetJsViewResult(string.Format("parent.List(1);parent.showTitle('{0}');parent.closeAppWindow1();", AppResource.SaveMessge));
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 删除
///
/// 删除
///
/// 结果
[HttpPost]
[HandleException]
public ActionResult Delete()
{
FileInfo file = new FileInfo();
string selectKey = Request.Form["selectKey"];
string[] list = selectKey.Split(":".ToCharArray());
int count = 0;
try
{
//调用服务
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
if (!string.IsNullOrEmpty(selectKey))
{
//删除附件
foreach (var item in list)
{
file.PID = item;
#region 调用服务
file = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_Get.ToString(), file);
#endregion
#region 调用服务
agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_DeleteFile.ToString(), file.FILESTORAGENAME);
#endregion
}
//删除文件信息
#region 调用服务
count = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_Delete.ToString(), selectKey);
#endregion
SetMessage(AppResource.DeleteMessage);
}
return List(true);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 上传附件
#region 上传附件
///
/// 上传附件
///
///
/// 附件路径,结果
[HttpPost]
public FileInfo UploadFile(HttpPostedFileBase file, string trueFilePath)
{
FileInfo model = new FileInfo();
model.IsSuccess = true;
string fileName = file.FileName;
try
{
#region 校验附件大小
//计算文件大小
int fileLength = file.ContentLength / 1024;
//判断如果文件大小超过5M,不允许上传
if (fileLength > 5120)
{
model.InfoError = "上传附件不能大于5M";
model.IsSuccess = false;
return model;
}
#endregion
//保存文件
file.SaveAs(trueFilePath);
model.FILESTORAGENAME = trueFilePath;
return model;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取文件保存路径
///
/// 获取文件保存路径
///
///
///
public string FilePath(string filePath)
{
string truefilepath = "";
string expandName = "";
string trueFileName = "sss";
#region 创建上传文件夹
//获取配置文件上传通知文件夹名称
string noticeUploadFile = ConfigurationManager.AppSettings.Get("UploadFileRoot");
//应用程序物理路径
// PhysicsRootPath = Server.MapPath(HttpRuntime.AppDomainAppVirtualPath);
PhysicsRootPath= GetRootPath();
//附件目录
string UploadFile = PhysicsRootPath + noticeUploadFile;
//如果文件夹不存在则创建文件夹
if (System.IO.Directory.Exists(UploadFile) == false)
{
System.IO.Directory.CreateDirectory(UploadFile);
}
//通知公告文件路径
string NoticePath = UploadFile + "\\FileInfo\\";
//如果文件夹不存在则创建文件夹
if (System.IO.Directory.Exists(NoticePath) == false)
{
System.IO.Directory.CreateDirectory(NoticePath);
}
#endregion
//构造文件名
if (filePath.IndexOf(".") > 0)
{
expandName = filePath.Substring(filePath.LastIndexOf(".") + 1);
string temp = filePath.Substring(filePath.LastIndexOf("."));
trueFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
trueFileName = trueFileName.Substring(0, trueFileName.Length - temp.Length);
truefilepath = NoticePath + trueFileName + "." + expandName;
}
return truefilepath;
}
#endregion
#region 截取附件名称
///
/// 截取附件名称
///
/// 附件路径
/// 附件名称
public string ChangeFilePath(string filePath)
{
string expandName = "";
string trueFileName = "sss";
//构造文件名
if (filePath.IndexOf(".") > 0)
{
expandName = filePath.Substring(filePath.LastIndexOf(".") + 1);
string temp = filePath.Substring(filePath.LastIndexOf("."));
trueFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
trueFileName = trueFileName.Substring(0, trueFileName.Length - temp.Length);
trueFileName = trueFileName + "." + expandName;
}
return trueFileName;
}
#endregion
#endregion
#region 下载附件
///
/// 下载文件名
///
///
[HttpPost]
public FilePathResult DownAttach(string fileName, string oldFileName)
{
try
{
if(string.IsNullOrEmpty(fileName))
{
fileName = oldFileName;
}
//应用程序物理路径
string PhysicsRootPath = GetRootPath(); //Server.MapPath(HttpRuntime.AppDomainAppVirtualPath);
string noticeUploadFile = "\\" + ConfigurationManager.AppSettings.Get("UploadFileRoot") + "\\";
string contentType = GetContenType(fileName.Substring(fileName.LastIndexOf(".")));
return File(PhysicsRootPath + noticeUploadFile + uploadFolder + fileName, contentType, Url.Encode(oldFileName));
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 根据后缀获取返回CONTENTTYPE
///
/// 根据后缀获取返回CONTENTTYPE
///
/// 后缀
/// CONTENTTYPE
private string GetContenType(string suffix)
{
string resultType = "";
if (".txt".Equals(suffix.ToLower()))
{
resultType = "";
}
switch (suffix.ToLower())
{
case ".txt":
resultType = "text/plain";
break;
case ".xls":
resultType = "application/vnd.ms-excel";
break;
case ".xlsx":
resultType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case ".doc":
resultType = "application/msword";
break;
case ".docx":
resultType = "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
break;
case ".ppt":
resultType = "application/vnd.ms-powerpoint";
break;
case ".pptx":
resultType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
break;
case ".bmp":
resultType = "application/x-MS-bmp";
break;
case ".jpg":
resultType = "aimage/jpeg";
break;
case ".jpeg":
resultType = "image/jpeg";
break;
case ".gif":
resultType = "image/gif";
break;
case ".png":
resultType = "image/png";
break;
case ".rar":
resultType = "application/x-rar-compressed";
break;
case ".zip":
resultType = "application/zip";
break;
}
return resultType;
}
#endregion
#region 获取通知类型下拉列表
///
/// 获取通知类型下拉列表
///
///
public ContentResult GetTypeComboxSource()
{
List list = new List();
try
{
#region 调用服务
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
list = agent.InvokeServiceFunction>(QMAPP.ServicesAgent.SystemService.FileTypeBLL_GetCombox.ToString());
#endregion
ComboboxResult model = new ComboboxResult();
foreach (var item in list)
{
model.Add(new ComboboxItem { ID = item.TYPENO, Text = item.TYPENAME });
}
return Content(model.ToString());
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 导出excel
///
/// 导出excel
///
/// 结果
//[HttpPost]
public ActionResult ExportExcel()
{
FileInfoModel seachModel = null;
FileInfo condition = null;
DataTable exportDt = new DataTable();
ServiceAgent wcfAgent = this.GetServiceAgent();
string ids = Request.Params["ids"];
try
{
//获取查询对象
seachModel = GetModel();
condition = CopyToModel(seachModel);
//类别列表选择的分类
condition.TYPENO = Request.Params["TYPENO"];
#region 调用服务
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
exportDt = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_GetExportData.ToString(), condition);
#endregion
#region 根据所选信息进行导出
//根据所选信息进行导出
if (ids != "")
{
DataView dv = new DataView(exportDt);
string strWhere = "";
string[] list = ids.Split(":".ToCharArray());
foreach (string ID in list)
{
strWhere += " PID='" + ID + "' or";
}
if (strWhere != "")
{
strWhere = strWhere.Remove((strWhere.Length - 2), 2);
}
dv.RowFilter = strWhere;
exportDt = dv.ToTable();
}
#endregion
//导出
QMFrameWork.WebUI.Util.IEFileTool efTool = new QMFrameWork.WebUI.Util.IEFileTool();
return efTool.GetExcelFileResult("FileInfo", "文件信息.xlsx", exportDt);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 取得网站根目录的物理路径
///
/// 取得网站根目录的物理路径
///
///
public static string GetRootPath()
{
string AppPath = "";
System.Web.HttpContext HttpCurrent = System.Web.HttpContext.Current;
if (HttpCurrent != null)
{
AppPath = HttpCurrent.Server.MapPath("~");
}
else
{
AppPath = AppDomain.CurrentDomain.BaseDirectory;
if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
AppPath = AppPath.Substring(0, AppPath.Length - 1);
}
return AppPath;
}
#endregion
#region 判断文件是否存在
///
/// 判断文件是否存在
///
///
///
///
public ActionResult ExistFile()
{
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
string fileName = Request.Params["fileName"];
string oldFileName = Request.Params["oldFileName"];
try
{
if (string.IsNullOrEmpty(fileName))
{
fileName = oldFileName;
}
//应用程序物理路径
//string PhysicsRootPath = Server.MapPath(HttpRuntime.AppDomainAppVirtualPath);
PhysicsRootPath = GetRootPath();
string noticeUploadFile = "\\" + ConfigurationManager.AppSettings.Get("UploadFileRoot") + "\\";
string contentType = GetContenType(fileName.Substring(fileName.LastIndexOf(".")));
//判断文件路径是否存在
string trueFilePath = PhysicsRootPath + noticeUploadFile + uploadFolder + fileName;
bool issuccess = agent.InvokeServiceFunction(QMAPP.ServicesAgent.SystemService.FileInfoBLL_ExistsFile.ToString(), trueFilePath);
if (!issuccess)
{
return Json("false");
}
else
{
return Json("true");
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}