supplierBuildFile.aspx.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using Aliyun.OSS;
  2. using Aliyun.OSS.Common;
  3. using BizCom;
  4. using ICSharpCode.SharpZipLib.Zip;
  5. using NPOI.OpenXmlFormats.Dml.Diagram;
  6. using NPOI.OpenXmlFormats.Shared;
  7. using SiteCore.Handler;
  8. using SiteCore.Redis;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Configuration;
  12. using System.Data;
  13. using System.Data.SqlClient;
  14. using System.Diagnostics;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Net;
  18. using System.Runtime.InteropServices;
  19. using System.Security.Principal;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using System.Web;
  24. using System.Web.Services.Description;
  25. using System.Web.UI;
  26. public partial class supplierBuildFile : System.Web.UI.Page
  27. {
  28. public static string upPath = ConfigurationManager.AppSettings["upPath"];
  29. public static string copyPath = ConfigurationManager.AppSettings["copyPath"];
  30. public static string siteUrl = ConfigurationManager.AppSettings["OriSiteUrl"];
  31. [DllImport("DrvInterface64.dll", CharSet = CharSet.Unicode)]
  32. public static extern uint DecFile(string filename);
  33. [DllImport("DrvInterface64.dll", CharSet = CharSet.Unicode)]
  34. public static extern int IsFileEncrypted(string filename);//返回1为加密,0为未被加密
  35. [DllImport("DrvInterface64.dll", CharSet = CharSet.Ansi)]
  36. public static extern void CreateUserKey(StringBuilder key, int len);
  37. [DllImport("DrvInterface64.dll", CharSet = CharSet.Ansi)]
  38. public static extern int InitAesKey(StringBuilder key, int len);
  39. [DllImport("DrvInterface64.dll")]
  40. public static extern int IsInitedAesKey();
  41. private void conErc(string msg)
  42. {
  43. XLog.SaveLog(0, msg);
  44. Response.Write(msg);
  45. //Response.StatusCode = (int)HttpStatusCode.NotFound;
  46. //Response.End();
  47. }
  48. private void conSuc(string msg)
  49. {
  50. Response.Write("{\"type\":\"success\",\"result\":\"" + msg + "\"}");
  51. //Response.End();
  52. }
  53. protected void Page_Load(object sender, EventArgs e)
  54. {
  55. if (!IsPostBack)
  56. {
  57. Response.Buffer = true;
  58. downloadMore();
  59. }
  60. }
  61. private string getCanDownFile(string fileName)
  62. {
  63. string[] extArr = new string[] { ".cdr", ".zip", ".rar", ".pdf" };
  64. foreach (string ext in extArr)
  65. {
  66. string fname = fileName + ext;
  67. if (File.Exists(fname))
  68. {
  69. return fname;
  70. }
  71. }
  72. return "";
  73. }
  74. static bool running = false;
  75. private void downloadMore()
  76. {
  77. if (running)
  78. {
  79. conSuc("运行中!");
  80. return;
  81. }
  82. running = true;
  83. var identity = WindowsIdentity.GetCurrent();
  84. var principal = new WindowsPrincipal(identity);
  85. Task.Run(async () =>
  86. {
  87. try
  88. {
  89. using (identity.Impersonate())
  90. {
  91. MemoryStream ms = new MemoryStream();
  92. byte[] buffer = null;
  93. List<string> addFiles = new List<string>();
  94. List<string> addCtid = new List<string>();
  95. using (ZipFile file = ZipFile.Create(ms))
  96. {
  97. }
  98. }
  99. running = false;
  100. }
  101. catch (Exception ex)
  102. {
  103. running = false;
  104. }
  105. });
  106. conSuc("运行结束!");
  107. //FileOssUp();
  108. }
  109. private string formatMemo(object memo)
  110. {
  111. string m = memo.ToString();
  112. m = m.Replace("\\", "").Replace("/", "").Replace(":", "").Replace("*", "").Replace("?", "").Replace("<", "").Replace(">", "").Replace("|", "");
  113. return m;
  114. }
  115. private void copyFile(string date, string supplier, string file)
  116. {
  117. string path = copyPath + "\\" + date + "\\" + supplier + "\\" + "车间下载";
  118. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  119. string fname = Path.GetFileName(file);
  120. File.Copy(file, path + "\\" + fname, true);
  121. if (!File.Exists(path + "\\" + fname))
  122. {
  123. File.Copy(file, path + "\\" + fname, true);
  124. }
  125. }
  126. private string getDesignTime(object v)
  127. {
  128. if (v.ToString() == "") return "";
  129. return Convert.ToDateTime(v).ToString("yyyyMMdd");
  130. }
  131. private string getDesignDate(object v)
  132. {
  133. return DateTime.Now.ToString("yyyy-MM-dd");
  134. /*if (v.ToString() == "") return "";
  135. return Convert.ToDateTime(v).ToString("yyyy-MM-dd");*/
  136. }
  137. private void FileOssUp()
  138. {
  139. // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
  140. var endpoint = "https://oss-cn-fuzhou.aliyuncs.com";
  141. // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  142. var accessKeyId = "LTAI5tE7wuNkeT9jZ42bQnyr";
  143. var accessKeySecret = "NnutvWhKl4HbQFSad3HosYbCkEkbjN";
  144. // 填写Bucket名称,例如examplebucket。
  145. var bucketName = "dfdiyfile";
  146. // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
  147. var objectName = "supplierFile/test.zip";
  148. // 填写Bucket所在地域对应的Region。以华东1(杭州)为例,Region填写为cn-hangzhou。
  149. string region = "cn-fuzhou";
  150. // 创建ClientConfiguration实例,按照您的需要修改默认参数。
  151. var conf = new ClientConfiguration();
  152. // 设置v4签名。
  153. conf.SignatureVersion = SignatureVersion.V4;
  154. // 创建OssClient实例。
  155. var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
  156. client.SetRegion(region);
  157. try
  158. {
  159. MemoryStream ms = new MemoryStream();
  160. byte[] buffer = null;
  161. using (ZipFile file = ZipFile.Create(ms))
  162. {
  163. file.BeginUpdate();
  164. //file.NameTransform = new ZipNameTransform();
  165. file.NameTransform = new MyNameTransfom();
  166. file.Add("C:\\Users\\231010\\Downloads\\setup.exe");
  167. file.CommitUpdate();
  168. buffer = new byte[ms.Length];
  169. ms.Position = 0;
  170. ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
  171. ms.Flush();
  172. ms.Close();
  173. }
  174. MemoryStream requestContent = new MemoryStream(buffer);
  175. // 上传文件。
  176. client.PutObject(bucketName, objectName, requestContent);
  177. Debug.WriteLine("Put object succeeded");
  178. }
  179. catch (Exception ex)
  180. {
  181. Debug.WriteLine("Put object failed" + ex.Message);
  182. }
  183. conSuc("上传成功!");
  184. return;
  185. }
  186. /// 批量进行多个文件压缩到一个文件
  187. /// </summary>
  188. /// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者
  189. /// <param name="zipFileName">生成的zip文件名称</param>
  190. private void ZipFileDownload(int userId, List<string> ctidLst, List<string> files, string zipFileName, int onlyDownFile, int isFromClient)
  191. {
  192. // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
  193. var endpoint = "https://oss-cn-fuzhou.aliyuncs.com";
  194. // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  195. var accessKeyId = "LTAI5tE7wuNkeT9jZ42bQnyr";
  196. var accessKeySecret = "NnutvWhKl4HbQFSad3HosYbCkEkbjN";
  197. // 填写Bucket名称,例如examplebucket。
  198. var bucketName = "dfdiyfile";
  199. // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
  200. var objectName = "exampledir/exampleobject.txt";
  201. // 填写字符串。
  202. var objectContent = "More than just cloud.";
  203. // 填写Bucket所在地域对应的Region。以华东1(杭州)为例,Region填写为cn-hangzhou。
  204. const string region = "oss-cn-fuzhou";
  205. // 创建ClientConfiguration实例,按照您的需要修改默认参数。
  206. var conf = new ClientConfiguration();
  207. // 设置v4签名。
  208. conf.SignatureVersion = SignatureVersion.V4;
  209. // 创建OssClient实例。
  210. var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
  211. client.SetRegion(region);
  212. try
  213. {
  214. MemoryStream ms = new MemoryStream();
  215. byte[] buffer = null;
  216. using (ZipFile file = ZipFile.Create(ms))
  217. {
  218. file.BeginUpdate();
  219. //file.NameTransform = new ZipNameTransform();
  220. file.NameTransform = new MyNameTransfom();
  221. file.Add("C:\\Users\\231010\\Downloads\\setup.exe");
  222. file.CommitUpdate();
  223. buffer = new byte[ms.Length];
  224. ms.Position = 0;
  225. ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
  226. ms.Flush();
  227. ms.Close();
  228. }
  229. MemoryStream requestContent = new MemoryStream(buffer);
  230. // 上传文件。
  231. client.PutObject(bucketName, objectName, requestContent);
  232. Console.WriteLine("Put object succeeded");
  233. }
  234. catch (Exception ex)
  235. {
  236. Console.WriteLine("Put object failed, {0}", ex.Message);
  237. }
  238. conSuc("上传成功!");
  239. return;
  240. //for (int i = 0; i < addFiles.Count; i++)
  241. //{
  242. // updateIsDownSuccess(userId, addCtid[i]);
  243. //}
  244. //StringBuilder sql = new StringBuilder();
  245. //string tids = string.Join(",", addCtid);
  246. //sql.AppendFormat("update CE_ErpTradeCell set OrderState = 6 where ctid in ({0}) ;", ("'" + tids.Replace(",", "','") + "'"));
  247. //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
  248. //sql = new StringBuilder();
  249. //sql.AppendFormat("insert into CE_ErpTradeLog(tid,orderstate,userid,operatetime,con) select ctid,{1},{2},getdate(),'{3}' from ce_erptradecell where ctid in ({0}) ;", ("'" + tids.Replace(",", "','") + "'"), (int)OrderState.下单完成, userId, "下载设计文件");
  250. //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
  251. }
  252. //public static object downfileObj = new object();
  253. private void downLoadFile(int userId, string ctid, string file, int onlyDownFile, int isFromClient)
  254. {
  255. string filePath = file;
  256. string dfile = Path.GetFileName(file);
  257. FileStream fs = new FileStream(filePath, FileMode.Open);
  258. byte[] bytes = new byte[(int)fs.Length];
  259. fs.Read(bytes, 0, bytes.Length);
  260. fs.Close();
  261. Response.Clear();
  262. //
  263. Response.ClearContent();
  264. Response.ClearHeaders();
  265. Response.ContentType = "application/octet-stream";
  266. //通知浏览器下载文件而不是打开\\fileDownload=true; path=/
  267. Response.AddHeader("Set-Cookie", "fileDownload=true; path=/;");
  268. //Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  269. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(dfile, System.Text.Encoding.UTF8));
  270. Response.AddHeader("Content-Length", bytes.Length.ToString());
  271. Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
  272. Response.BinaryWrite(bytes);
  273. Response.Flush();
  274. //updateIsDownSuccess(userId, ctid);
  275. //StringBuilder sql = new StringBuilder();
  276. //string tids = string.Join(",", ctid);
  277. //sql.AppendFormat("update CE_ErpTradeCell set OrderState = 6 where ctid in ({0}) ;", ("'" + tids.Replace(",", "','") + "'"));
  278. //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
  279. //sql = new StringBuilder();
  280. //sql.AppendFormat("insert into CE_ErpTradeLog(tid,orderstate,userid,operatetime,con) select ctid,{1},{2},getdate(),'{3}' from ce_erptradecell where ctid in ({0}) ;", ("'" + tids.Replace(",", "','") + "'"), (int)OrderState.下单完成, userId, "下载设计文件");
  281. //CeErpTradeCell.ExecuteNonQuery(sql.ToString());
  282. }
  283. public static void addLog(string ctid, int userid, string con, int orderState = 0, int aftersaleState = 0)
  284. {
  285. CeErpTradeLog log = new CeErpTradeLog();
  286. log.tid = ctid;
  287. log.UserId = userid;
  288. log.Con = con;
  289. log.OrderState = orderState;
  290. log.AfterSaleState = aftersaleState;
  291. log.OperateTime = DateTime.Now;
  292. log.Create();
  293. }
  294. }