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.

399 lines
16 KiB

2 years ago
  1. using EasyBL;
  2. using EasyBL.WEBAPP;
  3. using Entity.Sugar;
  4. using SqlSugar.Base;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Web;
  10. namespace WebApp
  11. {
  12. /// <summary>
  13. /// Controller 的摘要说明
  14. /// </summary>
  15. public class Controller : IHttpHandler
  16. {
  17. public void ProcessRequest(HttpContext context)
  18. {
  19. //Handler action = null;
  20. var req = context.Request;
  21. var sAction = req["action"];
  22. switch (sAction)
  23. {
  24. case "downfile":
  25. downFile(context);
  26. break;
  27. case "getimg":
  28. GetImg(context);
  29. break;
  30. case "saveimg":
  31. SaveImg(context);
  32. break;
  33. case "upload":
  34. Upload(context);
  35. break;
  36. case "importfile":
  37. PutImportFile(context);
  38. break;
  39. default:
  40. break;
  41. }
  42. //action.Process();
  43. }
  44. #region Upload 上傳文件
  45. public static void Upload(HttpContext c)
  46. {
  47. var req = c.Request;
  48. var sSource = req["source"];
  49. var sOrgID = req["orgid"];
  50. var sUserID = req["userid"];
  51. var sParentID = req["parentid"];
  52. var sServerPath = c.Server.MapPath("/");
  53. var sRoot = sServerPath + "Document\\EurotranFile";
  54. Common.FnCreateDir(sRoot + "\\" + sSource);//如果沒有該目錄就創建目錄
  55. if (req.Files.Count > 0)
  56. {
  57. var saFilesAdd = new List<OTB_SYS_Files>();
  58. for (int index = 0; index < req.Files.Count; index++)
  59. {
  60. var file = req.Files[index];
  61. var sFileSizeName = "";
  62. var sFileID = Guid.NewGuid().ToString();//檔案ID
  63. var sFileName = Path.GetFileName(file.FileName);//檔案名稱+文件格式名稱
  64. var sFileType = file.ContentType; //檔案類型
  65. var iFileSize = file.ContentLength; //檔案大小
  66. var KBSize = Math.Round((decimal)iFileSize / 1024, 1);//單位KB
  67. if (KBSize < 1024)
  68. {
  69. sFileSizeName = KBSize + "KB";
  70. }
  71. else
  72. {
  73. var ComparisonSize = Math.Round((decimal)KBSize / 1024, 1);//單位MB
  74. sFileSizeName = ComparisonSize + "MB";
  75. }
  76. var sfileName = sFileName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  77. var sSubFileName = sfileName.LastOrDefault(); //副檔名
  78. var sNewFileName = sFileID + '.' + sSubFileName;
  79. var sOutputPath = sRoot + "/" + sSource + "/" + sNewFileName;
  80. sOutputPath = System.Text.RegularExpressions.Regex.Replace(sOutputPath, @"//|/", @"\");
  81. file.SaveAs(sOutputPath);
  82. var oFile = new OTB_SYS_Files
  83. {
  84. OrgID = sOrgID,
  85. FileID = sFileID,
  86. ParentID = sParentID,
  87. SourceFrom = sSource,
  88. FileName = sFileName,
  89. SubFileName = sSubFileName,
  90. FilePath = sOutputPath.Replace(sServerPath, ""),
  91. FileType = sFileType,
  92. FileSize = iFileSize,
  93. FileSizeName = sFileSizeName,
  94. CreateUser = sUserID,
  95. CreateDate = DateTime.Now,
  96. ModifyUser = sUserID,
  97. ModifyDate = DateTime.Now
  98. };
  99. saFilesAdd.Add(oFile);
  100. }
  101. var db = SugarBase.DB;
  102. var iRes = db.Insertable(saFilesAdd).ExecuteCommand();
  103. if (iRes > 0)
  104. {
  105. var rp = c.Response;
  106. var sJsonText = ServiceBase.JsonToString(saFilesAdd);
  107. rp.Write(sJsonText);
  108. rp.End();
  109. }
  110. }
  111. }
  112. #endregion Upload 上傳文件
  113. #region downFile 下載文件
  114. public static void downFile(HttpContext c)
  115. {
  116. var req = c.Request;
  117. var sPath = req["path"];
  118. var sCusFileName = req["filename"];
  119. sPath = System.Text.RegularExpressions.Regex.Replace(sPath, @"//|/", @"\");
  120. using (var webService = new System.Web.Services.WebService())
  121. {
  122. var sServerPath = webService.Server.MapPath("/");
  123. sPath = sPath.Replace(sServerPath, "");
  124. using (var webService1 = new System.Web.Services.WebService())
  125. {
  126. sPath = webService1.Server.MapPath(sPath);
  127. var sFileName = "";
  128. var sExtName = "";
  129. if (sPath == null || !File.Exists(sPath))
  130. {
  131. return;
  132. }
  133. var index = 0;
  134. index = sPath.IndexOf("OutFiles");
  135. if (index == -1)
  136. {
  137. index = sPath.IndexOf("EurotranFile");
  138. }
  139. var sNewPath = sPath.Substring(index);
  140. var saPath = sNewPath.Split('.');
  141. var saNewPath = saPath[0].Split("\\/".ToCharArray());
  142. sExtName = saPath[1];
  143. sFileName = sCusFileName ?? saNewPath[saNewPath.Length - 1];
  144. c.Response.ContentType = "application/octet-stream";
  145. //设置响应的返回类型是文件流
  146. c.Response.AddHeader("content-disposition", "attachment;filename=" + sFileName + "." + sExtName);
  147. //设置相应的头以及被下载时候显示的文件名
  148. var lFileInfo = new FileInfo(sPath);
  149. //获取下载文件的文件流
  150. c.Response.WriteFile(lFileInfo.FullName);
  151. //返回要下载的文件流
  152. c.Response.End();
  153. }
  154. }
  155. }
  156. #endregion downFile 下載文件
  157. #region GetImg 獲取圖片
  158. public static void GetImg(HttpContext c)
  159. {
  160. var sOrgId = c.Request["orgid"];
  161. var sId = c.Request["id"];
  162. var sFolder = c.Request["folder"];
  163. var sImgsrc = "";
  164. var sSubFileName = "";
  165. var rp = c.Response;
  166. try
  167. {
  168. var isValidGuId = Guid.TryParse(sId, out Guid OutPutGuId);
  169. if (!string.IsNullOrEmpty(sId) && isValidGuId) //不為空
  170. {
  171. var db = SugarBase.GetIntance();
  172. var oFile = db.Queryable<OTB_SYS_Files>()
  173. .Where(it => it.ParentID == sId)
  174. .WhereIF(!string.IsNullOrWhiteSpace(sOrgId), it => it.OrgID == sOrgId).Single();
  175. if (oFile != null)
  176. {
  177. sImgsrc = oFile.FilePath;
  178. }
  179. if (!string.IsNullOrEmpty(sImgsrc))
  180. {
  181. //DB內找不到圖片
  182. sSubFileName = oFile.SubFileName == null ? "jpeg" : oFile.SubFileName;
  183. rp.ContentType = "image/" + sSubFileName;
  184. }
  185. else
  186. {
  187. sImgsrc = sFolder == "Members" ? WebAppGlobalConstWord.NOIMAGE : WebAppGlobalConstWord.NOIMG;
  188. rp.ContentType = "image/.jpg";
  189. rp.WriteFile(sImgsrc);
  190. }
  191. rp.WriteFile(sImgsrc);
  192. }
  193. else
  194. {
  195. sImgsrc = sFolder == "Members" ? WebAppGlobalConstWord.NOIMAGE : WebAppGlobalConstWord.NOIMG;
  196. rp.ContentType = "image/.jpg";
  197. rp.WriteFile(sImgsrc);
  198. }
  199. }
  200. catch (Exception)
  201. {
  202. sImgsrc = sFolder == "Members" ? WebAppGlobalConstWord.NOIMAGE : WebAppGlobalConstWord.NOIMG;
  203. rp.ContentType = "image/.jpg";
  204. rp.WriteFile(sImgsrc);
  205. }
  206. }
  207. #endregion GetImg 獲取圖片
  208. #region SaveImg 儲存圖片
  209. public static void SaveImg(HttpContext c)
  210. {
  211. c.Response.ContentType = "text/plain";
  212. var strfilepath = HttpUtility.UrlDecode(c.Request.Form["filepath"], System.Text.Encoding.UTF8);
  213. var phy = HttpUtility.UrlDecode(c.Request.Form["phy"], System.Text.Encoding.UTF8);
  214. var filename = HttpUtility.UrlDecode(c.Request.Form["filename"], System.Text.Encoding.UTF8);
  215. var isrepeat = HttpUtility.UrlDecode(c.Request.Form["isrepeat"], System.Text.Encoding.UTF8);
  216. var user = HttpUtility.UrlDecode(c.Request.Form["user"], System.Text.Encoding.UTF8);
  217. var oDocument_model = new OTB_SYS_Document();
  218. var TopPath = "Document";
  219. var fileroot = "";
  220. try
  221. {
  222. if (phy == "false")
  223. {
  224. //不是實體路徑
  225. //取得實體路徑
  226. strfilepath = HttpContext.Current.Request.MapPath(strfilepath);
  227. }
  228. fileroot = ("\\" + strfilepath.Substring(strfilepath.IndexOf(TopPath))).Replace("\\", "/");
  229. fileroot = fileroot.Substring(0, 1) != "/" ? "/" + fileroot : fileroot; //如果開頭沒有斜線就加上斜線
  230. fileroot = fileroot.Substring(fileroot.Length - 1) != "/" ? fileroot + "/" : fileroot; //如果尾端沒有斜線就加上斜線
  231. var f_name = Path.GetFileNameWithoutExtension(filename);
  232. var f_extn = Path.GetExtension(filename);
  233. var gu_id = Guid.NewGuid().ToString(); //新增圖片GUID
  234. var stringToBase64 = ""; //儲存縮小圖的base64字串
  235. oDocument_model.GUID = gu_id;
  236. oDocument_model.FileName = f_name;
  237. oDocument_model.SubFileName = f_extn;
  238. oDocument_model.FileRoot = fileroot;
  239. oDocument_model.FilePath = fileroot + filename;
  240. var newfile = new FileInfo(strfilepath + filename);
  241. oDocument_model.FileSize = (int)newfile.Length / 1024; //圖片大小
  242. #region base64string 縮圖處理
  243. using (var ms = new MemoryStream())
  244. {
  245. using (var fs = new FileStream(newfile.FullName, FileMode.Open))
  246. {
  247. using (var img = System.Drawing.Image.FromStream(fs))
  248. {
  249. oDocument_model.PixelW = img.Width;
  250. oDocument_model.PixelH = img.Height;
  251. var intW = 100;
  252. var intH = 100;
  253. if (img.Width > img.Height)
  254. {
  255. intW = 100;
  256. intH = (int)100 * img.Height / img.Width;
  257. }
  258. else
  259. {
  260. intH = 100;
  261. intW = (int)100 * img.Width / img.Height;
  262. }
  263. using (var NewImage = new System.Drawing.Bitmap(intW, intH))
  264. {
  265. using (var g = System.Drawing.Graphics.FromImage(NewImage))
  266. {
  267. g.DrawImage(img, 0, 0, intW, intH);
  268. NewImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  269. var imageBytes = ms.ToArray();
  270. stringToBase64 = Convert.ToBase64String(imageBytes);
  271. oDocument_model.FileContent = "data:image/jpg;base64," + stringToBase64;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. #endregion base64string 縮圖處理
  278. oDocument_model.IsProtected = "N"; //是否能複製、移除
  279. oDocument_model.IsPublic = "N"; //是否共用
  280. oDocument_model.Memo = (fileroot + filename).Replace("/", " "); //關鍵字查詢
  281. oDocument_model.FileCreateDate = newfile.CreationTime; //檔案創建日
  282. oDocument_model.CreateUser = !string.IsNullOrEmpty(user) ? user : "apadmin"; //創建人
  283. oDocument_model.ModifyUser = !string.IsNullOrEmpty(user) ? user : "apadmin"; //修改人
  284. var db = SugarBase.GetIntance();
  285. if (isrepeat == "true")
  286. {
  287. //修改
  288. var oDoc = db.Queryable<OTB_SYS_Document>().Single(it => it.FilePath == fileroot + filename && it.status != 99);
  289. if (oDoc != null)
  290. {
  291. //有查到GUID才更新圖檔
  292. var iEffect_Upd = db.Updateable(oDocument_model).Where(it => it.GUID == oDoc.GUID).ExecuteCommand();
  293. if (iEffect_Upd > 0)
  294. {
  295. c.Response.Write(oDocument_model.GUID); //儲存成功傳回圖檔GUID
  296. }
  297. }
  298. else
  299. {
  300. //沒有找到相對GUID就新增新增
  301. var iEffect_Add = db.Insertable(oDocument_model).ExecuteCommand();
  302. if (iEffect_Add > 0)
  303. {
  304. c.Response.Write(oDocument_model.GUID); //儲存成功傳回圖檔GUID
  305. }
  306. }
  307. }
  308. else
  309. {
  310. //新增
  311. var iEffect = db.Insertable(oDocument_model).ExecuteCommand();
  312. if (iEffect > 0)
  313. {
  314. c.Response.Write(oDocument_model.GUID); //儲存成功傳回圖檔GUID
  315. }
  316. }
  317. }
  318. catch (Exception ex)
  319. {
  320. LogService.mo_Log.Error("Controller.SaveImg Error Message:" + ex.Message, ex);
  321. c.Response.Write("0"); //儲存成功傳回0
  322. }
  323. }
  324. #endregion SaveImg 儲存圖片
  325. #region PutImportFile 將匯入的文件放在服務器上
  326. public static void PutImportFile(HttpContext c)
  327. {
  328. var req = c.Request;
  329. var sFileId = c.Request["FileId"];
  330. var sServerPath = HttpContext.Current.Server.MapPath("/");
  331. var sRoot = sServerPath + "OutFiles\\Temporary";
  332. Common.FnCreateDir(sRoot);//如果沒有該目錄就創建目錄
  333. var CheckFile = req.Files[0].FileName; //檔案名稱
  334. if (CheckFile != "")
  335. {
  336. var file = req.Files[0];
  337. var sFileName = Path.GetFileName(file.FileName);//檔案名稱+文件格式名稱
  338. var sfileName = sFileName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  339. var sSubFileName = sfileName.LastOrDefault(); //副檔名
  340. var sNewFileName = sFileId + '.' + sSubFileName;
  341. var sOutputPath = sRoot + "/" + sNewFileName;
  342. sOutputPath = System.Text.RegularExpressions.Regex.Replace(sOutputPath, @"//|/", @"\");
  343. if (File.Exists(sOutputPath))
  344. {
  345. File.Delete(sOutputPath);
  346. }
  347. file.SaveAs(sOutputPath);
  348. }
  349. }
  350. #endregion PutImportFile 將匯入的文件放在服務器上
  351. public bool IsReusable
  352. {
  353. get
  354. {
  355. return false;
  356. }
  357. }
  358. }
  359. }