Sys_AttachFiles_info_Ext.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using SysBaseLibs;
  3. using System.Web;
  4. using System.IO;
  5. namespace SysDataLibs.TableClass
  6. {
  7. public partial class Sys_AttachFiles_info
  8. {
  9. public bool UpdateFile(HttpFileCollection poFileUpload, UserSession poSession,bool isInsert=true)
  10. {
  11. //检查要上传的文件是否存在
  12. if (poFileUpload.Count <= 0)
  13. return false;
  14. bool lbRetVal = false;
  15. string uploadFileName = poFileUpload[0].FileName;
  16. if (uploadFileName.Trim() != "")
  17. {
  18. try
  19. {
  20. if (MyUtils.IsValidFileType(uploadFileName))
  21. {
  22. uploadFileName = uploadFileName.Substring(uploadFileName.LastIndexOf("\\", StringComparison.Ordinal) + 1);
  23. FileExt = uploadFileName.Substring(uploadFileName.LastIndexOf(".", StringComparison.Ordinal) + 1, uploadFileName.Length - uploadFileName.LastIndexOf(".", StringComparison.Ordinal) - 1);
  24. if (string.IsNullOrEmpty(FileTitle))
  25. FileTitle = uploadFileName.Remove(uploadFileName.LastIndexOf(".", StringComparison.Ordinal));
  26. FilePath = AppEnv.SysSetObj.GetString("DOWNLOADPATH") + "/" + AppEnv.SysSetObj.GetString("ATTACHFILES") + "/" + TableId.ToUpper();
  27. var uploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath;
  28. uploadFileDestination += FilePath;
  29. if (!Directory.Exists(uploadFileDestination))
  30. {
  31. Directory.CreateDirectory(uploadFileDestination);
  32. }
  33. string lcDateRd = FileTitle + "-" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + "." + FileExt;
  34. FileName = lcDateRd.Replace(" ", "");
  35. poFileUpload[0].SaveAs(uploadFileDestination + "/" + FileName);
  36. string s = isInsert ? InsertSql() : UpdateSql();
  37. lbRetVal = poSession.DBConn.ExcuteSqlTran(s);
  38. ThreadLog.LogInfo(poSession.UserInfo.Names + "(" + poSession.UserInfo.UserID + ")" + " Update File " + DownPath());
  39. //lbRetVal = true;
  40. }
  41. }
  42. catch (Exception e)
  43. {
  44. ThreadLog.LogException(e);
  45. ThreadLog.LogErr("文件上传异常");
  46. ThreadLog.LogException(e);
  47. lbRetVal = false;
  48. }
  49. }
  50. return lbRetVal;
  51. }
  52. public bool UpdateFileBase64(string base64Str, UserSession poSession, bool isInsert = true)
  53. {
  54. //检查要上传的文件是否存在
  55. bool lbRetVal = false;
  56. string uploadFileName = FileName;
  57. if (uploadFileName.Trim() != "")
  58. {
  59. try
  60. {
  61. FilePath = AppEnv.SysSetObj.GetString("DOWNLOADPATH") + "/" + AppEnv.SysSetObj.GetString("ATTACHFILES") + "/" + TableId.ToUpper();
  62. string fName = $"{FileName}-{DateTime.Now:yyMMddHHmmss}{new Random().Next(1000, 9999)}";
  63. var lcRetVal = Base64ToFile(base64Str, fName, FileExt, FilePath);
  64. // poFileUpload[0].SaveAs(uploadFileDestination + "/" + FileName);
  65. if (lcRetVal.StartsWith("error@"))
  66. {
  67. ThreadLog.LogInfo((lcRetVal.Split(new[] { '@' },
  68. StringSplitOptions.RemoveEmptyEntries)[1]));
  69. return false;
  70. }
  71. FileName = $"{fName}.{FileExt}";
  72. string s = isInsert ? InsertSql() : UpdateSql();
  73. lbRetVal = poSession.DBConn.ExcuteSqlTran(s);
  74. ThreadLog.LogInfo(poSession.UserInfo.Names + "(" + poSession.UserInfo.UserID + ")" + " Update File " + DownPath());
  75. //lbRetVal = true;
  76. }
  77. catch (Exception e)
  78. {
  79. ThreadLog.LogException(e);
  80. ThreadLog.LogErr("文件上传异常");
  81. ThreadLog.LogException(e);
  82. lbRetVal = false;
  83. }
  84. }
  85. return lbRetVal;
  86. }
  87. public static string Base64ToFile(string base64Str, string fileName, string fileExt, string filePath)
  88. {
  89. string lcRetVal = "error@";
  90. try
  91. {
  92. fileName = $"{fileName}.{fileExt}";
  93. filePath = filePath.StartsWith("/") ? filePath : ("/" + filePath);
  94. filePath = filePath.EndsWith("/") ? filePath : (filePath + "/");
  95. string path = $"{AppDomain.CurrentDomain.BaseDirectory}{filePath}";
  96. if (!Directory.Exists(path))
  97. Directory.CreateDirectory(path);
  98. byte[] bytes = Convert.FromBase64String(base64Str);
  99. using (FileStream fs = new FileStream($"{path}{fileName}", FileMode.Create, FileAccess.Write))
  100. {
  101. fs.Write(bytes, 0, bytes.Length);
  102. fs.Close();
  103. }
  104. lcRetVal = filePath + fileName;
  105. }
  106. catch (Exception e)
  107. {
  108. //typeof(SysAttachFileAppService).LogError(e);
  109. lcRetVal += "文件上传异常。";
  110. }
  111. return lcRetVal;
  112. }
  113. }
  114. }