using Learun.Application.Base.SystemModule;
using Learun.Application.OA.File.FilePreview;
using Learun.Util;
using System.Web;
using System.Web.Mvc;
namespace Learun.Application.Web.Areas.LR_SystemModule.Controllers
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.03.08
/// 描 述:附件管理
///
public class AnnexesController : MvcControllerBase
{
private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
#region 视图功能
///
/// 上传列表页面
///
///
[HttpGet]
public ActionResult UploadForm()
{
return View();
}
///
/// 下载列表页面
///
///
[HttpGet]
public ActionResult DownForm()
{
return View();
}
#endregion
#region 提交数据
///
/// 上传附件分片数据
///
/// 文件主键
/// 分片序号
/// 文件数据
///
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadAnnexesFileChunk(string fileGuid, int chunk, int chunks, HttpPostedFileBase Filedata)
{
//没有文件上传,直接返回
if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0)
{
if (Request.Files.Count > 0)
{
Filedata = Request.Files[0];
}
else
{
return HttpNotFound();
}
}
annexesFileIBLL.SaveChunkAnnexes(fileGuid, chunk, Filedata.InputStream);
return Success("保存成功");
}
///
/// 移除附件分片数据
///
/// 文件主键
/// 总分片数
///
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RemoveAnnexesFileChunk(string fileGuid, int chunks)
{
annexesFileIBLL.RemoveChunkAnnexes(fileGuid, chunks);
return Success("移除成功");
}
///
/// 合并上传附件的分片数据
///
/// 附件夹主键
/// 文件主键
/// 文件名
/// 文件总分片数
///
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MergeAnnexesFile(string folderId, string fileGuid, string fileName, int chunks, string filePath)
{
UserInfo userInfo = LoginUserInfo.Get();
string path = "";
if (!string.IsNullOrEmpty(filePath))
{
path = Config.GetValue(filePath);
//如果是相对路径先转换成绝对路径
if (path.Contains("~"))
{
path = Server.MapPath(path);
}
}
bool res = annexesFileIBLL.SaveAnnexes(folderId, fileGuid, fileName, chunks, userInfo, path);
if (res)
{
return Success("保存文件成功");
}
else
{
return Fail("保存文件失败");
}
}
///
/// 删除文件
///
/// 文件主键
///
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteAnnexesFile(string fileId)
{
AnnexesFileEntity fileInfoEntity = annexesFileIBLL.GetEntity(fileId);
annexesFileIBLL.DeleteEntity(fileId);
//删除文件
if (System.IO.File.Exists(fileInfoEntity.F_FilePath))
{
System.IO.File.Delete(fileInfoEntity.F_FilePath);
}
return Success("删除附件成功");
}
#endregion
#region 获取数据
///
/// 下载文件
///
/// 文件id
///
[HttpPost]
public void DownAnnexesFile(string fileId)
{
var data = annexesFileIBLL.GetEntity(fileId);
string filename = Server.UrlDecode(data.F_FileName);//返回客户端文件名称
string filepath = data.F_FilePath;
if (FileDownHelper.FileExists(filepath))
{
FileDownHelper.DownLoadold(filepath, filename);
}
}
///
/// 获取附件列表
///
/// 附件夹主键
///
[HttpGet]
public ActionResult GetAnnexesFileList(string folderId)
{
var data = annexesFileIBLL.GetList(folderId);
return JsonResult(data);
}
///
/// 获取附件夹信息
///
/// 附件夹主键
///
[HttpGet]
public ActionResult GetFileNames(string folderId)
{
var data = annexesFileIBLL.GetFileNames(folderId);
return Success(data);
}
#endregion
#region 预览附件
///
/// 文件预览
///
/// 文件ID
///
public void PreviewFile(string fileId)
{
FilePreviewIBLL filePreviewIBLL = new FilePreviewBLL();
var data = annexesFileIBLL.GetEntity(fileId);
if (data == null)
{
return;
}
string filename = data.F_FileName;//客户端保存的文件名
string filepath = data.F_FilePath;//路径
if (data.F_FileType == "xlsx" || data.F_FileType == "xls")
{
filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
if (!DirFileHelper.IsExistFile(filepath))
{
filePreviewIBLL.GetExcelData(data.F_FilePath);
}
}
if (data.F_FileType == "docx" || data.F_FileType == "doc")
{
filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
if (!DirFileHelper.IsExistFile(filepath))
{
filePreviewIBLL.GetWordData(data.F_FilePath);
}
}
if (data.F_FileType == "ppt" || data.F_FileType == "pptx")
{
filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
if (!DirFileHelper.IsExistFile(filepath))
{
filePreviewIBLL.GetPptData(data.F_FilePath);
}
}
Response.ClearContent();
switch (data.F_FileType)
{
case "jpg":
Response.ContentType = "image/jpeg";
break;
case "gif":
Response.ContentType = "image/gif";
break;
case "png":
Response.ContentType = "image/png";
break;
case "bmp":
Response.ContentType = "application/x-bmp";
break;
case "jpeg":
Response.ContentType = "image/jpeg";
break;
case "doc":
Response.ContentType = "application/pdf";
break;
case "docx":
Response.ContentType = "application/pdf";
break;
case "ppt":
Response.ContentType = "application/pdf";
break;
case "pptx":
Response.ContentType = "application/pdf";
break;
case "xls":
Response.ContentType = "application/pdf";
break;
case "xlsx":
Response.ContentType = "application/pdf";
break;
case "pdf":
Response.ContentType = "application/pdf";
break;
case "txt":
Response.ContentType = "text/plain";
break;
case "csv":
Response.ContentType = "";
break;
default:
Response.ContentType = "application/pdf";
break;
}
Response.Charset = "GB2312";
Response.WriteFile(filepath);
//Response.BinaryWrite(ms.ToArray());
}
#endregion
}
}