Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

232 rader
9.0 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Util;
  3. using Nancy;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. namespace Learun.Application.WebApi.Modules
  8. {
  9. public class AnnexesApiWx : BaseApi
  10. {
  11. public AnnexesApiWx()
  12. : base("/learun/adms/annexes")
  13. {
  14. Get["/wxlist"] = WxGetList;
  15. Get["/wxdown"] = WxDownload;
  16. Get["/wxfileinfo"] = WxFileInfo;
  17. Post["/wxupload"] = WxUpload;
  18. Post["/wxbase64"] = WxUploadbase64;
  19. Post["/wxdelete"] = WxDeleteFile;
  20. Post["/wxuploadinsingle"] = WxUploadInSingle;
  21. }
  22. private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
  23. /// <summary>
  24. /// 获取附件列表
  25. /// </summary>
  26. /// <param name="_"></param>
  27. /// <returns></returns>
  28. public Response WxGetList(dynamic _)
  29. {
  30. var keyValue = this.GetReqData();
  31. var list = annexesFileIBLL.GetList(keyValue);
  32. return Success(list);
  33. }
  34. public Response WxUploadbase64(dynamic _)
  35. {
  36. var files = (List<HttpFile>)this.Context.Request.Files;
  37. //string folderId = Request.Form["folderId"];
  38. //string filePath = Config.GetValue("AnnexesFile");
  39. //string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  40. //string fileEextension = Path.GetExtension(files[0].Name);
  41. //string fileType = fileEextension.Replace(".", "");
  42. //string fileGuid = Guid.NewGuid().ToString();
  43. byte[] bytes = new byte[files[0].Value.Length];
  44. files[0].Value.Read(bytes, 0, bytes.Length);
  45. string src = Convert.ToBase64String(bytes);
  46. //string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, fileEextension);
  47. ////创建文件夹
  48. //string path = Path.GetDirectoryName(virtualPath);
  49. //Directory.CreateDirectory(path);
  50. //AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  51. //if (!System.IO.File.Exists(virtualPath))
  52. //{
  53. // byte[] bytes = new byte[files[0].Value.Length];
  54. // files[0].Value.Read(bytes, 0, bytes.Length);
  55. // //FileInfo file = new FileInfo(virtualPath);
  56. // //FileStream fs = file.Create();
  57. // //fs.Write(bytes, 0, bytes.Length);
  58. // //fs.Close();
  59. // string src = "data:image/png;base64," + Convert.ToBase64String(bytes);
  60. // //virtualPath
  61. //}
  62. return SuccessString(src);
  63. }
  64. /// <summary>
  65. /// 上传附件图片文件
  66. /// <summary>
  67. /// <returns></returns>
  68. public Response WxUpload(dynamic _)
  69. {
  70. var files = (List<HttpFile>)this.Context.Request.Files;
  71. string folderId = Request.Form["folderId"];
  72. string filePath = Config.GetValue("AnnexesFile");
  73. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  74. string fileEextension = Path.GetExtension(files[0].Name);
  75. string fileType = fileEextension.Replace(".", "");
  76. string fileGuid = Guid.NewGuid().ToString();
  77. string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, fileEextension);
  78. //创建文件夹
  79. string path = Path.GetDirectoryName(virtualPath);
  80. Directory.CreateDirectory(path);
  81. AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  82. if (!System.IO.File.Exists(virtualPath))
  83. {
  84. byte[] bytes = new byte[files[0].Value.Length];
  85. files[0].Value.Read(bytes, 0, bytes.Length);
  86. FileInfo file = new FileInfo(virtualPath);
  87. FileStream fs = file.Create();
  88. fs.Write(bytes, 0, bytes.Length);
  89. fs.Close();
  90. //文件信息写入数据库
  91. fileAnnexesEntity.F_Id = fileGuid;
  92. fileAnnexesEntity.F_FileName = files[0].Name;
  93. fileAnnexesEntity.F_FilePath = virtualPath;
  94. fileAnnexesEntity.F_FileSize = files[0].Value.Length.ToString();
  95. fileAnnexesEntity.F_FileExtensions = fileEextension;
  96. fileAnnexesEntity.F_FileType = fileType;
  97. fileAnnexesEntity.F_CreateUserId = userInfo.userId;
  98. fileAnnexesEntity.F_CreateUserName = userInfo.realName;
  99. annexesFileIBLL.SaveEntity(folderId, fileAnnexesEntity);
  100. }
  101. return SuccessString(folderId);
  102. }
  103. /// <summary>
  104. /// 上传附件图片文件【移动端app2.0,单独页面中的附件图片上传使用方法】
  105. /// <summary>
  106. /// <returns></returns>
  107. public Response WxUploadInSingle(dynamic _)
  108. {
  109. var files = (List<HttpFile>)this.Context.Request.Files;
  110. var folderId = this.GetReq<FileModel>().folderId;
  111. var name = this.GetReq<FileModel>().name;
  112. string filePath = Config.GetValue("AnnexesFile");
  113. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  114. string FileEextension = Path.GetExtension(name);
  115. string fileGuid = Guid.NewGuid().ToString();
  116. string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, FileEextension);
  117. //创建文件夹
  118. string path = Path.GetDirectoryName(virtualPath);
  119. Directory.CreateDirectory(path);
  120. AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  121. if (!System.IO.File.Exists(virtualPath))
  122. {
  123. byte[] bytes = new byte[files[0].Value.Length];
  124. files[0].Value.Read(bytes, 0, bytes.Length);
  125. FileInfo file = new FileInfo(virtualPath);
  126. FileStream fs = file.Create();
  127. fs.Write(bytes, 0, bytes.Length);
  128. fs.Close();
  129. //文件信息写入数据库
  130. fileAnnexesEntity.F_Id = fileGuid;
  131. fileAnnexesEntity.F_FolderId = folderId;
  132. fileAnnexesEntity.F_FileName = name;
  133. fileAnnexesEntity.F_FilePath = virtualPath;
  134. fileAnnexesEntity.F_FileSize = files[0].Value.Length.ToString();
  135. fileAnnexesEntity.F_FileExtensions = FileEextension;
  136. fileAnnexesEntity.F_FileType = FileEextension.Replace(".", "");
  137. fileAnnexesEntity.F_CreateUserId = userInfo.userId;
  138. fileAnnexesEntity.F_CreateUserName = userInfo.realName;
  139. annexesFileIBLL.SaveEntity(folderId, fileAnnexesEntity);
  140. }
  141. return SuccessString(folderId);
  142. }
  143. /// <summary>
  144. /// 获取文件信息
  145. /// </summary>
  146. /// <param name="_"></param>
  147. /// <returns></returns>
  148. public Response WxFileInfo(dynamic _)
  149. {
  150. var fileId = this.GetReqData();
  151. var fileEntity = annexesFileIBLL.GetEntity(fileId);
  152. return Success(fileEntity);
  153. }
  154. /// <summary>
  155. /// 删除文件
  156. /// </summary>
  157. /// <param name="_"></param>
  158. /// <returns></returns>
  159. public Response WxDeleteFile(dynamic _)
  160. {
  161. var fileId = this.GetReqData();
  162. AnnexesFileEntity fileInfoEntity = annexesFileIBLL.GetEntity(fileId);
  163. if (fileInfoEntity != null)
  164. {
  165. annexesFileIBLL.DeleteEntity(fileId);
  166. //删除文件
  167. if (System.IO.File.Exists(fileInfoEntity.F_FilePath))
  168. {
  169. System.IO.File.Delete(fileInfoEntity.F_FilePath);
  170. }
  171. }
  172. return Success("删除成功");
  173. }
  174. /// <summary>
  175. /// 下载文件,微信小程序用
  176. ///
  177. /// 微信小程序可以预览图片、文档
  178. /// 支持的图片格式:.jpg .png .webp .gif
  179. /// 支持的文档格式:.doc(x) .xls(x) .ppt(x) .pdf
  180. ///
  181. /// 对于其他格式的文件,微信小程序官方未提供打开或预览的 API,文件对用户来说不可访问
  182. /// </summary>
  183. /// <param name="_"></param>
  184. /// <returns></returns>
  185. public Response WxDownload(dynamic _)
  186. {
  187. string name = this.GetReqData();
  188. string fileId = name.Split('.')[0];
  189. var fileEntity = annexesFileIBLL.GetEntity(fileId);
  190. string filepath = fileEntity.F_FilePath;
  191. if (!FileDownHelper.FileExists(filepath))
  192. {
  193. return 404;
  194. }
  195. FileDownHelper.DownLoadWx(filepath, fileEntity.F_FileType);
  196. return Success("");
  197. }
  198. public class FileModel
  199. {
  200. public string folderId { get; set; }
  201. public string name { get; set; }
  202. public string id { get; set; }
  203. }
  204. }
  205. }