You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

280 lines
9.8 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Application.OA.File.FilePreview;
  3. using Learun.Util;
  4. using Quanjiang.DigitalSchool.AsposeHelper;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. namespace Learun.Application.Web.Areas.LR_SystemModule.Controllers
  8. {
  9. /// <summary>
  10. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  11. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  12. /// 创建人:陈彬彬
  13. /// 日 期:2017.03.08
  14. /// 描 述:附件管理
  15. /// </summary>
  16. public class AnnexesController : MvcControllerBase
  17. {
  18. private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
  19. #region 视图功能
  20. /// <summary>
  21. /// 上传列表页面
  22. /// </summary>
  23. /// <returns></returns>
  24. [HttpGet]
  25. public ActionResult UploadForm()
  26. {
  27. return View();
  28. }
  29. /// <summary>
  30. /// 下载列表页面
  31. /// </summary>
  32. /// <returns></returns>
  33. [HttpGet]
  34. public ActionResult DownForm()
  35. {
  36. return View();
  37. }
  38. #endregion
  39. #region 提交数据
  40. /// <summary>
  41. /// 上传附件分片数据
  42. /// </summary>
  43. /// <param name="fileGuid">文件主键</param>
  44. /// <param name="chunk">分片序号</param>
  45. /// <param name="Filedata">文件数据</param>
  46. /// <returns></returns>
  47. [HttpPost]
  48. [ValidateAntiForgeryToken]
  49. public ActionResult UploadAnnexesFileChunk(string fileGuid, int chunk, int chunks, HttpPostedFileBase Filedata)
  50. {
  51. //没有文件上传,直接返回
  52. if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0)
  53. {
  54. if (Request.Files.Count > 0)
  55. {
  56. Filedata = Request.Files[0];
  57. }
  58. else
  59. {
  60. return HttpNotFound();
  61. }
  62. }
  63. annexesFileIBLL.SaveChunkAnnexes(fileGuid, chunk, Filedata.InputStream);
  64. return Success("保存成功");
  65. }
  66. /// <summary>
  67. /// 移除附件分片数据
  68. /// </summary>
  69. /// <param name="fileGuid">文件主键</param>
  70. /// <param name="chunks">总分片数</param>
  71. /// <returns></returns>
  72. [HttpPost]
  73. [ValidateAntiForgeryToken]
  74. public ActionResult RemoveAnnexesFileChunk(string fileGuid, int chunks)
  75. {
  76. annexesFileIBLL.RemoveChunkAnnexes(fileGuid, chunks);
  77. return Success("移除成功");
  78. }
  79. /// <summary>
  80. /// 合并上传附件的分片数据
  81. /// </summary>
  82. /// <param name="folderId">附件夹主键</param>
  83. /// <param name="fileGuid">文件主键</param>
  84. /// <param name="fileName">文件名</param>
  85. /// <param name="chunks">文件总分片数</param>
  86. /// <returns></returns>
  87. [HttpPost]
  88. [ValidateAntiForgeryToken]
  89. public ActionResult MergeAnnexesFile(string folderId, string fileGuid, string fileName, int chunks, string filePath)
  90. {
  91. UserInfo userInfo = LoginUserInfo.Get();
  92. string path = "";
  93. if (!string.IsNullOrEmpty(filePath))
  94. {
  95. path = Config.GetValue(filePath);
  96. //如果是相对路径先转换成绝对路径
  97. if (path.Contains("~"))
  98. {
  99. path = Server.MapPath(path);
  100. }
  101. }
  102. bool res = annexesFileIBLL.SaveAnnexes(folderId, fileGuid, fileName, chunks, userInfo, path);
  103. if (res)
  104. {
  105. return Success("保存文件成功");
  106. }
  107. else
  108. {
  109. return Fail("保存文件失败");
  110. }
  111. }
  112. /// <summary>
  113. /// 删除文件
  114. /// </summary>
  115. /// <param name="fileId">文件主键</param>
  116. /// <returns></returns>
  117. [HttpPost]
  118. [ValidateAntiForgeryToken]
  119. public ActionResult DeleteAnnexesFile(string fileId)
  120. {
  121. AnnexesFileEntity fileInfoEntity = annexesFileIBLL.GetEntity(fileId);
  122. annexesFileIBLL.DeleteEntity(fileId);
  123. //删除文件
  124. if (System.IO.File.Exists(fileInfoEntity.F_FilePath))
  125. {
  126. System.IO.File.Delete(fileInfoEntity.F_FilePath);
  127. }
  128. return Success("删除附件成功");
  129. }
  130. #endregion
  131. #region 获取数据
  132. /// <summary>
  133. /// 下载文件
  134. /// </summary>
  135. /// <param name="fileId">文件id</param>
  136. /// <returns></returns>
  137. [HttpPost]
  138. public void DownAnnexesFile(string fileId)
  139. {
  140. var data = annexesFileIBLL.GetEntity(fileId);
  141. string filename = Server.UrlDecode(data.F_FileName);//返回客户端文件名称
  142. string filepath = data.F_FilePath;
  143. if (FileDownHelper.FileExists(filepath))
  144. {
  145. FileDownHelper.DownLoadold(filepath, filename);
  146. }
  147. }
  148. /// <summary>
  149. /// 获取附件列表
  150. /// </summary>
  151. /// <param name="folderId">附件夹主键</param>
  152. /// <returns></returns>
  153. [HttpGet]
  154. public ActionResult GetAnnexesFileList(string folderId)
  155. {
  156. var data = annexesFileIBLL.GetList(folderId);
  157. return JsonResult(data);
  158. }
  159. /// <summary>
  160. /// 获取附件夹信息
  161. /// </summary>
  162. /// <param name="folderId">附件夹主键</param>
  163. /// <returns></returns>
  164. [HttpGet]
  165. public ActionResult GetFileNames(string folderId)
  166. {
  167. var data = annexesFileIBLL.GetFileNames(folderId);
  168. return Success(data);
  169. }
  170. #endregion
  171. #region 预览附件
  172. /// <summary>
  173. /// 文件预览
  174. /// </summary>
  175. /// <param name="fileId">文件ID</param>
  176. /// <returns></returns>
  177. public void PreviewFile(string fileId)
  178. {
  179. FilePreviewIBLL filePreviewIBLL = new FilePreviewBLL();
  180. var data = annexesFileIBLL.GetEntity(fileId);
  181. if (data == null)
  182. {
  183. return;
  184. }
  185. string filename = data.F_FileName;//客户端保存的文件名
  186. string filepath = data.F_FilePath;//路径
  187. if (data.F_FileType == "xlsx" || data.F_FileType == "xls")
  188. {
  189. filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
  190. if (!DirFileHelper.IsExistFile(filepath))
  191. {
  192. //filePreviewIBLL.GetExcelData(data.F_FilePath);
  193. //liang 2021-6-25 改aspose预览
  194. AsposeCore.GetExcelData(data.F_FilePath);
  195. }
  196. }
  197. if (data.F_FileType == "docx" || data.F_FileType == "doc")
  198. {
  199. filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
  200. if (!DirFileHelper.IsExistFile(filepath))
  201. {
  202. //filePreviewIBLL.GetWordData(data.F_FilePath);
  203. //liang 2021-6-25 改aspose预览
  204. AsposeCore.GetWordData(data.F_FilePath);
  205. }
  206. }
  207. if (data.F_FileType == "ppt" || data.F_FileType == "pptx")
  208. {
  209. filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
  210. if (!DirFileHelper.IsExistFile(filepath))
  211. {
  212. //filePreviewIBLL.GetPptData(data.F_FilePath);
  213. //liang 2021-6-25 改aspose预览
  214. AsposeCore.GetWordData(data.F_FilePath);
  215. }
  216. }
  217. Response.ClearContent();
  218. switch (data.F_FileType.ToLower())
  219. {
  220. case "jpg":
  221. Response.ContentType = "image/jpeg";
  222. break;
  223. case "gif":
  224. Response.ContentType = "image/gif";
  225. break;
  226. case "png":
  227. Response.ContentType = "image/png";
  228. break;
  229. case "bmp":
  230. Response.ContentType = "application/x-bmp";
  231. break;
  232. case "jpeg":
  233. Response.ContentType = "image/jpeg";
  234. break;
  235. case "doc":
  236. Response.ContentType = "application/pdf";
  237. break;
  238. case "docx":
  239. Response.ContentType = "application/pdf";
  240. break;
  241. case "ppt":
  242. Response.ContentType = "application/pdf";
  243. break;
  244. case "pptx":
  245. Response.ContentType = "application/pdf";
  246. break;
  247. case "xls":
  248. Response.ContentType = "application/pdf";
  249. break;
  250. case "xlsx":
  251. Response.ContentType = "application/pdf";
  252. break;
  253. case "pdf":
  254. Response.ContentType = "application/pdf";
  255. break;
  256. case "txt":
  257. Response.ContentType = "text/plain";
  258. break;
  259. case "csv":
  260. Response.ContentType = "";
  261. break;
  262. default:
  263. Response.ContentType = "application/pdf";
  264. break;
  265. }
  266. //Response.Charset = "GB2312";
  267. Response.Charset = "utf-8"; //修改txt文件预览乱码
  268. Response.WriteFile(filepath);
  269. //Response.BinaryWrite(ms.ToArray());
  270. }
  271. #endregion
  272. }
  273. }