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.
 
 
 
 
 
 

615 lines
21 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Application.OA.File.FileFolder;
  3. using Learun.Application.OA.File.FileInfo;
  4. using Learun.Application.OA.File.FilePreview;
  5. using Learun.Util;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. using Quanjiang.DigitalSchool.AsposeHelper;
  14. namespace Learun.Application.Web.Areas.LR_OAModule.Controllers
  15. {
  16. /// <summary>
  17. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  18. /// Copyright (c) 2013-2017
  19. /// 创建人:陈彬彬
  20. /// 日 期:2018.06.19
  21. /// 描 述:文件管理
  22. /// </summary>
  23. public class ResourceFileController : MvcControllerBase
  24. {
  25. private FileFolderIBLL fileFolderBLL = new FileFolderBLL();
  26. private FileInfoIBLL fileInfoBLL = new FileInfoBLL();
  27. private FilePreviewIBLL filePreviewIBLL = new FilePreviewBLL();
  28. private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
  29. #region 视图功能
  30. /// <summary>
  31. /// 文件管理
  32. /// </summary>
  33. /// <returns></returns>
  34. [HttpGet]
  35. public ActionResult Index()
  36. {
  37. return View();
  38. }
  39. /// <summary>
  40. /// 文件管理
  41. /// </summary>
  42. /// <returns></returns>
  43. [HttpGet]
  44. public ActionResult BcIndex()
  45. {
  46. return View();
  47. }
  48. /// <summary>
  49. /// 上传文件
  50. /// </summary>
  51. /// <returns></returns>
  52. [HttpGet]
  53. public ActionResult UploadifyForm()
  54. {
  55. return View();
  56. }
  57. /// <summary>
  58. /// 文件夹表单
  59. /// </summary>
  60. /// <returns></returns>
  61. [HttpGet]
  62. public ActionResult FolderForm()
  63. {
  64. return View();
  65. }
  66. /// <summary>
  67. /// 文件表单
  68. /// </summary>
  69. /// <returns></returns>
  70. [HttpGet]
  71. public ActionResult FileForm()
  72. {
  73. return View();
  74. }
  75. /// <summary>
  76. /// 文件(夹)移动表单
  77. /// </summary>
  78. /// <returns></returns>
  79. [HttpGet]
  80. public ActionResult MoveForm()
  81. {
  82. return View();
  83. }
  84. /// <summary>
  85. /// 云盘文件统计
  86. /// </summary>
  87. /// <returns></returns>
  88. [HttpGet]
  89. public ActionResult FileStatistic()
  90. {
  91. return View();
  92. }
  93. #endregion
  94. #region 获取数据
  95. /// <summary>
  96. /// 文件夹列表
  97. /// </summary>
  98. /// <returns>返回树形Json</returns>
  99. [HttpGet]
  100. public ActionResult GetTreeJson()
  101. {
  102. string userId = LoginUserInfo.Get().userId;
  103. var data = fileFolderBLL.GetList(userId);
  104. var treeList = new List<TreeModel>();
  105. foreach (FileFolderEntity item in data)
  106. {
  107. TreeModel tree = new TreeModel();
  108. bool hasChildren = data.Count(t => t.F_ParentId == item.F_FolderId) == 0 ? false : true;
  109. tree.id = item.F_FolderId;
  110. tree.text = item.F_FolderName;
  111. tree.value = item.F_FolderId;
  112. tree.parentId = item.F_ParentId;
  113. tree.isexpand = true;
  114. tree.complete = true;
  115. tree.hasChildren = hasChildren;
  116. if (hasChildren == false)
  117. {
  118. tree.icon = "fa fa-folder";
  119. }
  120. treeList.Add(tree);
  121. }
  122. return JsonResult(treeList);
  123. }
  124. /// <summary>
  125. /// 所有文件(夹)列表
  126. /// </summary>
  127. /// <param name="folderId">文件夹Id</param>
  128. /// <returns>返回列表Json</returns>
  129. [HttpGet]
  130. public ActionResult GetListJson(string folderId,string keyword)
  131. {
  132. string userId = LoginUserInfo.Get().userId;
  133. var data = fileInfoBLL.GetList(folderId, userId);
  134. return JsonResult(data);
  135. }
  136. /// <summary>
  137. /// 获取页面显示列表数据
  138. /// </summary>
  139. /// <param name="pagination">分页参数</param>
  140. /// <param name="queryJson">查询参数</param>
  141. /// <returns></returns>
  142. [HttpGet]
  143. [AjaxOnly]
  144. public ActionResult GetPageList(string folderId, string queryJson)
  145. {
  146. string userId = LoginUserInfo.Get().userId;
  147. var data = fileInfoBLL.GetList(folderId, userId, queryJson);
  148. return JsonResult(data);
  149. }
  150. /// <summary>
  151. /// 文档列表
  152. /// </summary>
  153. /// <returns>返回列表Json</returns>
  154. [HttpGet]
  155. public ActionResult GetDocumentListJson()
  156. {
  157. string userId = LoginUserInfo.Get().userId;
  158. var data = fileInfoBLL.GetDocumentList(userId);
  159. return JsonResult(data);
  160. }
  161. /// <summary>
  162. /// 图片列表
  163. /// </summary>
  164. /// <returns>返回列表Json</returns>
  165. [HttpGet]
  166. public ActionResult GetImageListJson()
  167. {
  168. string userId = LoginUserInfo.Get().userId;
  169. var data = fileInfoBLL.GetImageList(userId);
  170. return JsonResult(data);
  171. }
  172. /// <summary>
  173. /// 回收站文件(夹)列表
  174. /// </summary>
  175. /// <returns>返回列表Json</returns>
  176. [HttpGet]
  177. public ActionResult GetRecycledListJson()
  178. {
  179. string userId = LoginUserInfo.Get().userId;
  180. var data = fileInfoBLL.GetRecycledList(userId);
  181. return JsonResult(data);
  182. }
  183. /// <summary>
  184. /// 我的文件(夹)共享列表
  185. /// </summary>
  186. /// <returns>返回列表Json</returns>
  187. [HttpGet]
  188. public ActionResult GetMyShareListJson()
  189. {
  190. string userId = LoginUserInfo.Get().userId;
  191. var data = fileInfoBLL.GetMyShareList(userId);
  192. return JsonResult(data);
  193. }
  194. /// <summary>
  195. /// 他人文件(夹)共享列表
  196. /// </summary>
  197. /// <returns>返回列表Json</returns>
  198. [HttpGet]
  199. public ActionResult GetOthersShareListJson()
  200. {
  201. string userId = LoginUserInfo.Get().userId;
  202. var data = fileInfoBLL.GetOthersShareList(userId);
  203. return JsonResult(data);
  204. }
  205. /// <summary>
  206. /// 文件夹实体
  207. /// </summary>
  208. /// <param name="keyValue">主键值</param>
  209. /// <returns>返回对象Json</returns>
  210. [HttpGet]
  211. public ActionResult GetFolderFormJson(string keyValue)
  212. {
  213. var data = fileFolderBLL.GetEntity(keyValue);
  214. return JsonResult(data);
  215. }
  216. /// <summary>
  217. /// 文件实体
  218. /// </summary>
  219. /// <param name="keyValue">主键值</param>
  220. /// <returns>返回对象Json</returns>
  221. [HttpGet]
  222. public ActionResult GetFileFormJson(string keyValue)
  223. {
  224. var data = fileInfoBLL.GetEntity(keyValue);
  225. return JsonResult(data);
  226. }
  227. /// <summary>
  228. /// 获取云盘文件统计数据
  229. /// </summary>
  230. /// <param name="queryJson"></param>
  231. /// <returns></returns>
  232. public ActionResult GetFileStatisitcData(string queryJson)
  233. {
  234. var data = fileInfoBLL.GetListByJson(queryJson);
  235. var dataGroup = data.Where(x => x.F_CreateDate.HasValue).GroupBy(x => x.F_CreateDate.Value.Month).Select(x => new TempClass()
  236. {
  237. month=x.Key,
  238. count = x.Select(y=>y.F_FileId).Count()
  239. });
  240. var xAxis = new List<string>();
  241. var seriesData = new List<int>();
  242. for (int i = 0; i < 12; i++)
  243. {
  244. xAxis.Add(string.Format("{0}月", i + 1));
  245. var aa = dataGroup.FirstOrDefault(x => x.month == (i + 1));
  246. seriesData.Add(aa == null ? 0 : aa.count);
  247. }
  248. var jsonData = new
  249. {
  250. seriesData = seriesData,
  251. xAxis = xAxis
  252. };
  253. return Success(jsonData);
  254. }
  255. public class TempClass {
  256. public int month { get; set; }
  257. public int count { get; set; }
  258. }
  259. #endregion
  260. #region 提交数据
  261. /// <summary>
  262. /// 还原文件(夹)
  263. /// </summary>
  264. /// <param name="keyValue">主键值</param>
  265. /// <param name="fileType">文件类型</param>
  266. /// <returns></returns>
  267. [HttpPost]
  268. [AjaxOnly]
  269. public ActionResult RestoreFile(string keyValue, string fileType)
  270. {
  271. if (fileType == "folder")
  272. {
  273. fileFolderBLL.RestoreFile(keyValue);
  274. }
  275. else
  276. {
  277. fileInfoBLL.RestoreFile(keyValue);
  278. }
  279. return Success("还原成功。");
  280. }
  281. /// <summary>
  282. /// 删除文件(夹)
  283. /// </summary>
  284. /// <param name="keyValue">主键值</param>
  285. /// <param name="fileType">文件类型</param>
  286. /// <returns></returns>
  287. [HttpPost]
  288. [AjaxOnly]
  289. public ActionResult RemoveForm(string keyValue, string fileType)
  290. {
  291. if (fileType == "folder")
  292. {
  293. fileFolderBLL.RemoveForm(keyValue);
  294. }
  295. else
  296. {
  297. fileInfoBLL.RemoveForm(keyValue);
  298. }
  299. return Success("删除成功。");
  300. }
  301. /// <summary>
  302. /// 彻底删除文件(夹)
  303. /// </summary>
  304. /// <param name="keyValue">主键值</param>
  305. /// <param name="fileType">文件类型</param>
  306. /// <returns></returns>
  307. [HttpPost]
  308. [AjaxOnly]
  309. public ActionResult ThoroughRemoveForm(string keyValue, string fileType)
  310. {
  311. if (fileType == "folder")
  312. {
  313. fileFolderBLL.ThoroughRemoveForm(keyValue);
  314. }
  315. else
  316. {
  317. fileInfoBLL.ThoroughRemoveForm(keyValue);
  318. }
  319. return Success("删除成功。");
  320. }
  321. /// <summary>
  322. /// 清空回收站
  323. /// </summary>
  324. /// <returns></returns>
  325. [HttpPost]
  326. [AjaxOnly]
  327. public ActionResult EmptyRecycledForm()
  328. {
  329. fileFolderBLL.EmptyRecycledForm();
  330. return Success("操作成功。");
  331. }
  332. /// <summary>
  333. /// 保存文件夹表单(新增、修改)
  334. /// </summary>
  335. /// <param name="keyValue">主键值</param>
  336. /// <param name="fileFolderEntity">文件夹实体</param>
  337. /// <returns></returns>
  338. [HttpPost]
  339. [AjaxOnly]
  340. public ActionResult SaveFolderForm(string keyValue, FileFolderEntity fileFolderEntity)
  341. {
  342. fileFolderBLL.SaveForm(keyValue, fileFolderEntity);
  343. return Success("操作成功。");
  344. }
  345. /// <summary>
  346. /// 保存文件表单(新增、修改)
  347. /// </summary>
  348. /// <param name="keyValue">主键值</param>
  349. /// <param name="fileInfoEntity">文件实体</param>
  350. /// <returns></returns>
  351. [HttpPost]
  352. [AjaxOnly]
  353. public ActionResult SaveFileForm(string keyValue, FileInfoEntity fileInfoEntity)
  354. {
  355. fileInfoBLL.SaveForm(keyValue, fileInfoEntity);
  356. return Success("操作成功。");
  357. }
  358. /// <summary>
  359. /// 保存文件(夹)移动位置
  360. /// </summary>
  361. /// <param name="keyValue">主键值</param>
  362. /// <param name="moveFolderId">要移动文件夹Id</param>
  363. /// <param name="fileType">文件类型</param>
  364. /// <returns></returns>
  365. [HttpPost]
  366. [AjaxOnly]
  367. public ActionResult SaveMoveForm(string keyValue, string moveFolderId, string fileType)
  368. {
  369. if (fileType == "folder")
  370. {
  371. FileFolderEntity fileFolderEntity = new FileFolderEntity();
  372. fileFolderEntity.F_FolderId = keyValue;
  373. fileFolderEntity.F_ParentId = moveFolderId;
  374. fileFolderBLL.SaveForm(keyValue, fileFolderEntity);
  375. }
  376. else
  377. {
  378. FileInfoEntity fileInfoEntity = new FileInfoEntity();
  379. fileInfoEntity.F_FileId = keyValue;
  380. fileInfoEntity.F_FolderId = moveFolderId;
  381. fileInfoBLL.SaveForm(keyValue, fileInfoEntity);
  382. }
  383. return Success("操作成功。");
  384. }
  385. /// <summary>
  386. /// 共享文件(夹)
  387. /// </summary>
  388. /// <param name="keyValue">主键值</param>
  389. /// <param name="fileType">文件类型</param>
  390. /// <param name="IsShare">是否共享:1-共享 0取消共享</param>
  391. /// <returns></returns>
  392. [HttpPost]
  393. [AjaxOnly]
  394. public ActionResult ShareFile(string keyValue, int IsShare, string fileType)
  395. {
  396. if (fileType == "folder")
  397. {
  398. fileFolderBLL.ShareFolder(keyValue, IsShare);
  399. }
  400. else
  401. {
  402. fileInfoBLL.ShareFile(keyValue, IsShare);
  403. }
  404. return Success("共享成功。");
  405. }
  406. /// <summary>
  407. /// 上传文件
  408. /// </summary>
  409. /// <param name="folderId">文件夹Id</param>
  410. /// <param name="Filedata">文件对象</param>
  411. /// <returns></returns>
  412. [HttpPost]
  413. public ActionResult UploadifyFile(string folderId, HttpPostedFileBase Filedata)
  414. {
  415. try
  416. {
  417. Thread.Sleep(500);////延迟500毫秒
  418. //没有文件上传,直接返回
  419. if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0)
  420. {
  421. if (Request.Files.Count > 0)
  422. {
  423. Filedata = Request.Files[0];
  424. }
  425. else
  426. {
  427. return HttpNotFound();
  428. }
  429. }
  430. //获取文件完整文件名(包含绝对路径)
  431. //文件存放路径格式:/Resource/ResourceFile/{userId}{data}/{guid}.{后缀名}
  432. string userId = LoginUserInfo.Get().userId;
  433. string fileGuid = Guid.NewGuid().ToString();
  434. long filesize = Filedata.ContentLength;
  435. string FileEextension = Path.GetExtension(Filedata.FileName);
  436. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  437. string virtualPath = string.Format("~/Resource/DocumentFile/{0}/{1}/{2}{3}", userId, uploadDate, fileGuid, FileEextension);
  438. string fullFileName = this.Server.MapPath(virtualPath);
  439. //创建文件夹
  440. string path = Path.GetDirectoryName(fullFileName);
  441. Directory.CreateDirectory(path);
  442. if (!System.IO.File.Exists(fullFileName))
  443. {
  444. //保存文件
  445. Filedata.SaveAs(fullFileName);
  446. //文件信息写入数据库
  447. FileInfoEntity fileInfoEntity = new FileInfoEntity();
  448. fileInfoEntity.Create();
  449. fileInfoEntity.F_FileId = fileGuid;
  450. if (!string.IsNullOrEmpty(folderId))
  451. {
  452. fileInfoEntity.F_FolderId = folderId;
  453. }
  454. else
  455. {
  456. fileInfoEntity.F_FolderId = "0";
  457. }
  458. fileInfoEntity.F_FileName = Filedata.FileName;
  459. fileInfoEntity.F_FilePath = virtualPath;
  460. fileInfoEntity.F_FileSize = filesize.ToString();
  461. fileInfoEntity.F_FileExtensions = FileEextension;
  462. fileInfoEntity.F_FileType = FileEextension.Replace(".", "");
  463. fileInfoBLL.SaveForm("", fileInfoEntity);
  464. }
  465. return Success("上传成功。");
  466. }
  467. catch (Exception ex)
  468. {
  469. return Fail(ex.Message);
  470. }
  471. }
  472. /// <summary>
  473. /// 下载文件
  474. /// </summary>
  475. /// <param name="keyValue">主键</param>
  476. /// <returns></returns>
  477. [HttpPost]
  478. public void DownloadFile(string keyValue)
  479. {
  480. var data = fileInfoBLL.GetEntity(keyValue);
  481. string filename = Server.UrlDecode(data.F_FileName);//返回客户端文件名称
  482. string filepath = this.Server.MapPath(data.F_FilePath);
  483. if (FileDownHelper.FileExists(filepath))
  484. {
  485. FileDownHelper.DownLoadold(filepath, filename);
  486. }
  487. }
  488. #endregion
  489. #region
  490. /// <summary>
  491. /// 文件预览
  492. /// </summary>
  493. /// <param name="fileId">文件ID</param>
  494. /// <returns></returns>
  495. public void PreviewFile(string fileId)
  496. {
  497. var data = fileInfoBLL.GetEntity(fileId);
  498. if (data == null)
  499. {
  500. return;
  501. }
  502. string filename = Server.UrlDecode(data.F_FileName);//客户端保存的文件名
  503. string filepath = DirFileHelper.GetAbsolutePath(data.F_FilePath);//路径
  504. if (data.F_FileType == "xlsx" || data.F_FileType == "xls")
  505. {
  506. filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
  507. if (!DirFileHelper.IsExistFile(filepath))
  508. {
  509. //filePreviewIBLL.GetExcelData(DirFileHelper.GetAbsolutePath(data.F_FilePath));
  510. //liang 2021-6-25 改aspose预览
  511. AsposeCore.GetExcelData(DirFileHelper.GetAbsolutePath(data.F_FilePath));
  512. }
  513. }
  514. if (data.F_FileType == "docx" || data.F_FileType == "doc")
  515. {
  516. filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
  517. if (!DirFileHelper.IsExistFile(filepath))
  518. {
  519. //filePreviewIBLL.GetWordData(DirFileHelper.GetAbsolutePath(data.F_FilePath));
  520. //liang 2021-6-25 改aspose预览
  521. AsposeCore.GetWordData(DirFileHelper.GetAbsolutePath(data.F_FilePath));
  522. }
  523. }
  524. if (data.F_FileType == "ppt" || data.F_FileType == "pptx")
  525. {
  526. filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
  527. if (!DirFileHelper.IsExistFile(filepath))
  528. {
  529. //filePreviewIBLL.GetPptData(DirFileHelper.GetAbsolutePath(data.F_FilePath));
  530. //liang 2021-6-25 改aspose预览
  531. AsposeCore.GetWordData(DirFileHelper.GetAbsolutePath(data.F_FilePath));
  532. }
  533. }
  534. //FileDownHelper.DownLoadold(filepath, filename);
  535. //FileStream files = new FileStream(filepath, FileMode.Open);
  536. //byte[] fileByte = new byte[files.Length];
  537. //files.Read(fileByte, 0, fileByte.Length);
  538. //files.Close();
  539. //System.IO.MemoryStream ms = new MemoryStream(fileByte, 0, fileByte.Length);
  540. Response.ClearContent();
  541. switch (data.F_FileType)
  542. {
  543. case "jpg":
  544. Response.ContentType = "image/jpeg";
  545. break;
  546. case "gif":
  547. Response.ContentType = "image/gif";
  548. break;
  549. case "png":
  550. Response.ContentType = "image/png";
  551. break;
  552. case "bmp":
  553. Response.ContentType = "application/x-bmp";
  554. break;
  555. case "jpeg":
  556. Response.ContentType = "image/jpeg";
  557. break;
  558. case "doc":
  559. Response.ContentType = "application/pdf";
  560. break;
  561. case "docx":
  562. Response.ContentType = "application/pdf";
  563. break;
  564. case "ppt":
  565. Response.ContentType = "application/x-ppt";
  566. break;
  567. case "pptx":
  568. Response.ContentType = "application/x-ppt";
  569. break;
  570. case "xls":
  571. Response.ContentType = "application/pdf";
  572. break;
  573. case "xlsx":
  574. Response.ContentType = "application/pdf";
  575. break;
  576. case "pdf":
  577. Response.ContentType = "application/pdf";
  578. break;
  579. case "txt":
  580. Response.ContentType = "text/plain";
  581. break;
  582. case "csv":
  583. Response.ContentType = "";
  584. break;
  585. default:
  586. Response.ContentType = "application/pdf";
  587. break;
  588. }
  589. Response.Charset = "GB2312";
  590. Response.WriteFile(filepath);
  591. //Response.BinaryWrite(ms.ToArray());
  592. }
  593. #endregion
  594. }
  595. }