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.
 
 
 
 
 
 

281 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. if (chunks > 100)
  64. {
  65. return Fail("上传失败,文件超出可上传大小限制");
  66. }
  67. annexesFileIBLL.SaveChunkAnnexes(fileGuid, chunk, Filedata.InputStream);
  68. return Success("保存成功");
  69. }
  70. /// <summary>
  71. /// 移除附件分片数据
  72. /// </summary>
  73. /// <param name="fileGuid">文件主键</param>
  74. /// <param name="chunks">总分片数</param>
  75. /// <returns></returns>
  76. [HttpPost]
  77. [ValidateAntiForgeryToken]
  78. public ActionResult RemoveAnnexesFileChunk(string fileGuid, int chunks)
  79. {
  80. annexesFileIBLL.RemoveChunkAnnexes(fileGuid, chunks);
  81. return Success("移除成功");
  82. }
  83. /// <summary>
  84. /// 合并上传附件的分片数据
  85. /// </summary>
  86. /// <param name="folderId">附件夹主键</param>
  87. /// <param name="fileGuid">文件主键</param>
  88. /// <param name="fileName">文件名</param>
  89. /// <param name="chunks">文件总分片数</param>
  90. /// <returns></returns>
  91. [HttpPost]
  92. [ValidateAntiForgeryToken]
  93. public ActionResult MergeAnnexesFile(string folderId, string fileGuid, string fileName, int chunks, string filePath)
  94. {
  95. UserInfo userInfo = LoginUserInfo.Get();
  96. string path = "";
  97. if (!string.IsNullOrEmpty(filePath))
  98. {
  99. path = Config.GetValue(filePath);
  100. //如果是相对路径先转换成绝对路径
  101. if (path.Contains("~"))
  102. {
  103. path = Server.MapPath(path);
  104. }
  105. }
  106. bool res = annexesFileIBLL.SaveAnnexes(folderId, fileGuid, fileName, chunks, userInfo, path);
  107. if (res)
  108. {
  109. return Success("保存文件成功");
  110. }
  111. else
  112. {
  113. return Fail("保存文件失败");
  114. }
  115. }
  116. /// <summary>
  117. /// 删除文件
  118. /// </summary>
  119. /// <param name="fileId">文件主键</param>
  120. /// <returns></returns>
  121. [HttpPost]
  122. [ValidateAntiForgeryToken]
  123. public ActionResult DeleteAnnexesFile(string fileId)
  124. {
  125. AnnexesFileEntity fileInfoEntity = annexesFileIBLL.GetEntity(fileId);
  126. annexesFileIBLL.DeleteEntity(fileId);
  127. //删除文件
  128. if (System.IO.File.Exists(fileInfoEntity.F_FilePath))
  129. {
  130. System.IO.File.Delete(fileInfoEntity.F_FilePath);
  131. }
  132. return Success("删除附件成功");
  133. }
  134. #endregion
  135. #region 获取数据
  136. /// <summary>
  137. /// 下载文件
  138. /// </summary>
  139. /// <param name="fileId">文件id</param>
  140. /// <returns></returns>
  141. [HttpPost]
  142. public void DownAnnexesFile(string fileId)
  143. {
  144. var data = annexesFileIBLL.GetEntity(fileId);
  145. string filename = Server.UrlDecode(data.F_FileName);//返回客户端文件名称
  146. string filepath = data.F_FilePath;
  147. if (FileDownHelper.FileExists(filepath))
  148. {
  149. FileDownHelper.DownLoadold(filepath, filename);
  150. }
  151. }
  152. /// <summary>
  153. /// 获取附件列表
  154. /// </summary>
  155. /// <param name="folderId">附件夹主键</param>
  156. /// <returns></returns>
  157. [HttpGet]
  158. public ActionResult GetAnnexesFileList(string folderId)
  159. {
  160. var data = annexesFileIBLL.GetList(folderId);
  161. return JsonResult(data);
  162. }
  163. /// <summary>
  164. /// 获取附件夹信息
  165. /// </summary>
  166. /// <param name="folderId">附件夹主键</param>
  167. /// <returns></returns>
  168. [HttpGet]
  169. public ActionResult GetFileNames(string folderId)
  170. {
  171. var data = annexesFileIBLL.GetFileNames(folderId);
  172. return Success(data);
  173. }
  174. #endregion
  175. #region 预览附件
  176. /// <summary>
  177. /// 文件预览
  178. /// </summary>
  179. /// <param name="fileId">文件ID</param>
  180. /// <returns></returns>
  181. public void PreviewFile(string fileId)
  182. {
  183. FilePreviewIBLL filePreviewIBLL = new FilePreviewBLL();
  184. var data = annexesFileIBLL.GetEntity(fileId);
  185. if (data == null)
  186. {
  187. return;
  188. }
  189. string filename = data.F_FileName;//客户端保存的文件名
  190. string filepath = data.F_FilePath;//路径
  191. if (data.F_FileType == "xlsx" || data.F_FileType == "xls")
  192. {
  193. filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
  194. if (!DirFileHelper.IsExistFile(filepath))
  195. {
  196. AsposeCore.GetExcelData(data.F_FilePath);
  197. //filePreviewIBLL.GetExcelData(data.F_FilePath);
  198. }
  199. }
  200. if (data.F_FileType == "docx" || data.F_FileType == "doc")
  201. {
  202. filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
  203. if (!DirFileHelper.IsExistFile(filepath))
  204. {
  205. AsposeCore.GetWordData(data.F_FilePath);
  206. //filePreviewIBLL.GetWordData(data.F_FilePath);
  207. }
  208. }
  209. if (data.F_FileType == "ppt" || data.F_FileType == "pptx")
  210. {
  211. filepath = filepath.Substring(0, filepath.LastIndexOf(".")) + ".pdf";//文件名
  212. if (!DirFileHelper.IsExistFile(filepath))
  213. {
  214. AsposeCore.GetWordData(data.F_FilePath);
  215. //filePreviewIBLL.GetPptData(data.F_FilePath);
  216. }
  217. }
  218. Response.ClearContent();
  219. switch (data.F_FileType)
  220. {
  221. case "jpg":
  222. Response.ContentType = "image/jpeg";
  223. break;
  224. case "gif":
  225. Response.ContentType = "image/gif";
  226. break;
  227. case "png":
  228. Response.ContentType = "image/png";
  229. break;
  230. case "bmp":
  231. Response.ContentType = "application/x-bmp";
  232. break;
  233. case "jpeg":
  234. Response.ContentType = "image/jpeg";
  235. break;
  236. case "doc":
  237. Response.ContentType = "application/pdf";
  238. break;
  239. case "docx":
  240. Response.ContentType = "application/pdf";
  241. break;
  242. case "ppt":
  243. Response.ContentType = "application/pdf";
  244. break;
  245. case "pptx":
  246. Response.ContentType = "application/pdf";
  247. break;
  248. case "xls":
  249. Response.ContentType = "application/pdf";
  250. break;
  251. case "xlsx":
  252. Response.ContentType = "application/pdf";
  253. break;
  254. case "pdf":
  255. Response.ContentType = "application/pdf";
  256. break;
  257. case "txt":
  258. Response.ContentType = "text/plain";
  259. break;
  260. case "csv":
  261. Response.ContentType = "";
  262. break;
  263. default:
  264. Response.ContentType = "application/pdf";
  265. break;
  266. }
  267. Response.Charset = "GB2312";
  268. Response.Charset = "utf-8"; //修改txt文件预览乱码
  269. Response.WriteFile(filepath);
  270. //Response.BinaryWrite(ms.ToArray());
  271. }
  272. #endregion
  273. }
  274. }