uploadFile.aspx.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. using BizCom;
  2. using ICSharpCode.SharpZipLib.Zip;
  3. using Microsoft.Win32;
  4. using SevenZip;
  5. using System;
  6. using System.Configuration;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Text;
  10. using System.Web;
  11. using Utils;
  12. using SiteCore;
  13. using Aspose.Imaging.ImageOptions;
  14. using Aspose.Imaging;
  15. using Microsoft.WindowsAPICodePack.Shell;
  16. using System.Drawing.Imaging;
  17. using CorelDRAW;
  18. using System.Security.Principal;
  19. using System.Threading.Tasks;
  20. using System.Data;
  21. using System.Text.RegularExpressions;
  22. using MSharp.Framework;
  23. using System.Net.Http.Headers;
  24. using System.Net.Http;
  25. public partial class uploadFile : System.Web.UI.Page
  26. {
  27. public static string upPath = ConfigurationManager.AppSettings["upPath"];
  28. public static string curPath = ConfigurationManager.AppSettings["curPath"];
  29. static CdrConvert cdrConvert = new CdrConvert();
  30. private void conSuc(string msg)
  31. {
  32. Response.Write("{\"res\":\"1\",\"msg\":\"" + msg + "!\"}");
  33. //Response.End();
  34. }
  35. private void conErc(string msg)
  36. {
  37. Response.Write("{\"res\":\"0\",\"msg\":\"" + msg + "!\"}");
  38. //Response.End();
  39. }
  40. public string GetMD5HashFromFile(HttpPostedFile file)
  41. {
  42. try
  43. {
  44. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  45. byte[] retVal = md5.ComputeHash(file.InputStream);
  46. StringBuilder sb = new StringBuilder();
  47. for (int i = 0; i < retVal.Length; i++)
  48. {
  49. sb.Append(retVal[i].ToString("x2"));
  50. }
  51. return sb.ToString();
  52. }
  53. catch (Exception ex)
  54. {
  55. XLog.SaveLog(0, "上传MD5错误!" + ex.Message);
  56. }
  57. return "";
  58. }
  59. private static byte[] GetBlobByHttpPostedFile(HttpPostedFile httpPostedFile)
  60. {
  61. var contentLength = httpPostedFile.ContentLength;
  62. var result = new byte[contentLength];
  63. var inputStream = httpPostedFile.InputStream;
  64. inputStream.Read(result, 0, contentLength);
  65. return result;
  66. }
  67. protected void Page_Load(object sender, EventArgs e)
  68. {
  69. //if (CurrentUser == null)
  70. //{
  71. // Response.Write(err);
  72. // return;
  73. //}
  74. //CeErpTradeCell.GetByCtid("1717009344450030601");
  75. if (Request.Files.Count < 1)
  76. {
  77. conErc("空文件!");
  78. return;
  79. }
  80. int userId = 0;
  81. int orgid = 0;
  82. if (Request["userid"] != null)
  83. {
  84. userId = Convert.ToInt32(Request["userid"]);
  85. }
  86. if (Request["orgid"] != null)
  87. {
  88. orgid = Convert.ToInt32(Request["orgid"]);
  89. }
  90. HttpPostedFile postFile = Request.Files[0];
  91. if (postFile != null)
  92. {
  93. string file_name = postFile.FileName;
  94. string namePattern = @"《(.*?)》";
  95. Regex nameReg = new Regex(namePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline, TimeSpan.FromSeconds(2));//2秒后超时
  96. MatchCollection nameMatches = nameReg.Matches(file_name);//设定要查找的字符串
  97. if (nameMatches.Count > 0)
  98. {
  99. foreach (Match match in nameMatches)
  100. {
  101. file_name = file_name.Replace(match.Value, "");
  102. }
  103. }
  104. file_name = file_name.Replace("(", "(");
  105. file_name = file_name.Replace(")", ")");
  106. string ctid = MidStrEx(file_name, "(", ")").Trim();
  107. if (string.IsNullOrEmpty(ctid))
  108. {
  109. conErc("上传的文件名格式不正确");
  110. return;
  111. }
  112. string memoCtid = ctid;
  113. if (ctid.IndexOf("C") == -1)
  114. {
  115. if (file_name.IndexOf("[") != -1 && file_name.IndexOf("C") != -1)
  116. {
  117. if (ctid.IndexOf("S_") != -1) // (S_S_1962772776865084101)[C1] 对应ctid是 S_S_C1_1962772776865084101
  118. {
  119. int lastIndex = ctid.LastIndexOf("S_"); //最后一个S_的位置
  120. string sPre = ctid.Substring(0, lastIndex + 2); //S_S_
  121. string initTid = ctid.Substring(lastIndex + 2, ctid.Length - lastIndex - 2); //1962772776865084101
  122. string pre_ctid = MidStrEx(file_name, "[", "]"); //C1
  123. if (pre_ctid.IndexOf("+") != -1)
  124. {
  125. pre_ctid = "C" + pre_ctid.Split('+')[1];
  126. }
  127. memoCtid = sPre + pre_ctid + "_" + initTid; //S_S_ + C1 + _ +1962772776865084101
  128. }
  129. else
  130. {
  131. string pre_ctid = MidStrEx(file_name, "[", "]");
  132. if (pre_ctid.IndexOf("+") != -1)
  133. {
  134. pre_ctid = "C" + pre_ctid.Split('+')[1];
  135. }
  136. memoCtid = pre_ctid + "_" + ctid;
  137. }
  138. }
  139. }
  140. CeErpTradeCell entity = null;
  141. if (ctid != "") entity = CeErpTradeCell.GetByCode(ctid);
  142. if (entity == null) entity = CeErpTradeCell.GetByCtid(ctid);
  143. if (entity == null) entity = CeErpTradeCell.GetByCtid(memoCtid);
  144. try
  145. {
  146. if (entity != null)
  147. {
  148. string pname = Path.GetFileNameWithoutExtension(file_name);
  149. if (entity.IsRefund == 2)
  150. {
  151. StringBuilder sql = new StringBuilder();
  152. sql.AppendFormat("select refund_status from CE_ErpTradeOrder where tid='{0}'", entity.tid);
  153. DataTable dt = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  154. bool isAll = true;
  155. foreach (DataRow dr in dt.Rows)
  156. {
  157. if ("NO_REFUND".Equals(dr["refund_status"]))
  158. {
  159. isAll = false;
  160. break;
  161. }
  162. }
  163. if (isAll)
  164. {
  165. conErc("此单退款,不允许上传");
  166. return;
  167. }
  168. }
  169. if (entity.seller_memo != pname)
  170. {
  171. conErc("上传的文件名与备注不符合!");
  172. return;
  173. }
  174. if (entity.OrderState == -1)
  175. {
  176. conErc("还未审核不允许上传");
  177. return;
  178. }
  179. if (entity.OrderState < (int)OrderState.设计中 && orgid != 10 && orgid != 4)
  180. {
  181. conErc("还未开始设计不允许上传");
  182. return;
  183. }
  184. //if (pname.IndexOf("现货") != -1)
  185. //{
  186. // conErc("设计款的单文件名不能有现货字眼");
  187. // return;
  188. //}
  189. //更新状态
  190. if (entity.OrderState >= (int)OrderState.下单完成)
  191. {
  192. conErc("已经下单无法上传!");
  193. return;
  194. }
  195. if (entity.IsVerifyToSupplier)
  196. {
  197. conErc("订单已到车间无法上传!");
  198. return;
  199. }
  200. string extend = Path.GetExtension(file_name).ToLower();
  201. if (!(extend == ".cdr" || extend == ".zip" || extend == ".rar" || extend == ".pdf"))
  202. {
  203. conErc("只允许上传zip和cdr文件!");
  204. return;
  205. }
  206. if (entity.OrderState != 5)
  207. {
  208. entity.FinishDesignTime = DateTime.Now;
  209. if (entity.isDianziOrder == 1 || entity.ProductId == 57 || entity.ProductId == 28)
  210. {
  211. entity.OrderState = 6;
  212. entity.SupplierId = 35;
  213. entity.FinishPlaceTime = DateTime.Now;
  214. bool sendResult = commonHelper.SetOrderDummyDelivery(entity.tid);
  215. if (sendResult)
  216. {
  217. entity.OrderState = 7;
  218. }
  219. }
  220. else
  221. entity.OrderState = 5; //设计完成
  222. //if (entity.IsReturn == 2) //2是下单人打回给设计的,重新上传的话,需要清除打回
  223. //{
  224. entity.IsXianHuo = 0;
  225. entity.IsVerifyToSupplier = false;
  226. //}
  227. }
  228. entity.IsReturn = 0;
  229. int num = getProductCount(entity.seller_memo);
  230. if (entity.MemoOpt == 1 || entity.MemoOpt == 2)
  231. {
  232. entity.MemoOpt = 0;
  233. }
  234. if (entity.payment < 500)
  235. {
  236. if (entity.seller_memo.Contains("插卡") && entity.ProductCount != null && !entity.seller_memo.Contains("S_"))
  237. {
  238. if (num >= 100)
  239. {
  240. entity.IsVerifyToSupplier = true;
  241. entity.SupplierId = 3;
  242. entity.FinishPlaceTime = DateTime.Now;
  243. }
  244. }
  245. if ((((entity.seller_memo.Contains("条幅彩色") && !entity.seller_memo.Contains("辽宁") && !entity.seller_memo.Contains("山东")) || entity.seller_memo.Contains("贡锻布") || entity.seller_memo.Contains("贡缎布") || entity.seller_memo.Contains("旗帜布")) && !entity.seller_memo.Contains("双喷")) || (entity.seller_memo.Contains("帆布") && !entity.seller_memo.Contains("帆布袋")))
  246. {
  247. entity.IsVerifyToSupplier = true;
  248. entity.SupplierId = 98;
  249. entity.FinishPlaceTime = DateTime.Now;
  250. }
  251. }
  252. string patterns = "出货|加急|顺丰|打印|专版|当天发";
  253. if (!Regex.IsMatch(entity.seller_memo, patterns) && string.IsNullOrEmpty(entity.OtherMemo) && !entity.ctid.Contains("S_"))
  254. {
  255. if (entity.payment <= 300 && num >= 200 && num <= 3000 && entity.ShopId != 14 && entity.ShopId != 99)
  256. {
  257. //300克铜板纸/铜版纸300克-覆哑膜/覆膜/不覆膜-直角/裁切
  258. if (!entity.seller_memo.Contains("opp") && !entity.seller_memo.Contains("按文件") && !entity.seller_memo.Contains("流苏") && !entity.seller_memo.Contains("排序") && !entity.seller_memo.Contains("烫金"))
  259. {
  260. if ((entity.seller_memo.Contains("直角") || entity.seller_memo.Contains("裁切")) && (entity.seller_memo.Contains("300克铜板纸") || entity.seller_memo.Contains("300克铜板纸")) && (entity.seller_memo.Contains("覆哑膜") || entity.seller_memo.Contains("覆膜") || entity.seller_memo.Contains("不覆膜")))
  261. {
  262. entity.IsVerifyToSupplier = true;
  263. entity.SupplierId = 14;
  264. entity.FinishPlaceTime = DateTime.Now;
  265. }
  266. }
  267. }
  268. if (entity.payment <= 100 && num > 500 && num <= 1500)
  269. {
  270. //数量0-1500
  271. //0-80*54
  272. string pattern = @"\b(\d{1,5}[x\*]\d{1,5}(mm|cm))\b";
  273. string memo = entity.seller_memo.Replace("MM", "mm").Replace("CM", "cm");
  274. Regex reg = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline, TimeSpan.FromSeconds(2));//2秒后超时
  275. MatchCollection matches = reg.Matches(memo);//设定要查找的字符串
  276. string size = "";
  277. double min_width = 80;
  278. double min_height = 54;
  279. double width = 0;
  280. double height = 0;
  281. bool isIn = false;
  282. try
  283. {
  284. if (matches[0].Success)
  285. {
  286. size = matches[0].Groups[0].Value;
  287. size = size.Replace("mm", "");
  288. size = size.Replace("cm", "");
  289. string[] size_list = size.Split('x');
  290. if (size_list.Length > 1)
  291. {
  292. width = Convert.ToDouble(size_list[0]);
  293. height = Convert.ToDouble(size_list[1]);
  294. }
  295. if ((width <= min_width && height <= min_height) || (height <= min_width && width <= min_height))
  296. {
  297. isIn = true;
  298. }
  299. if (width < 30 || height < 30)
  300. {
  301. isIn = false;
  302. }
  303. }
  304. }
  305. catch (Exception ex)
  306. {
  307. }
  308. //铜版纸不干胶-覆亮膜
  309. if (isIn && entity.seller_memo.Contains("铜版纸不干胶") && entity.seller_memo.Contains("覆亮膜") && !entity.seller_memo.Contains("烫金") && !entity.seller_memo.Contains("排序"))
  310. {
  311. entity.IsVerifyToSupplier = true;
  312. entity.SupplierId = 64;
  313. entity.FinishPlaceTime = DateTime.Now;
  314. }
  315. }
  316. if (entity.payment <= 500 && num > 200 && num <= 5000)
  317. {
  318. //数量0-5000
  319. if (entity.seller_memo.Contains("透明不干胶"))
  320. {
  321. entity.IsVerifyToSupplier = true;
  322. entity.SupplierId = 64;
  323. entity.FinishPlaceTime = DateTime.Now;
  324. }
  325. }
  326. if (entity.payment <= 100 && num > 200 && num <= 1000)
  327. {
  328. //数量0-1000
  329. if (entity.seller_memo.Contains("铜版纸不干胶") && entity.seller_memo.Contains("覆哑膜"))
  330. {
  331. entity.IsVerifyToSupplier = true;
  332. entity.SupplierId = 64;
  333. entity.FinishPlaceTime = DateTime.Now;
  334. }
  335. }
  336. if (entity.payment <= 500 && num > 0 && num <= 50)
  337. {
  338. //数量0-50
  339. if (entity.seller_memo.Contains("领淘550灯布海报") || entity.seller_memo.Contains("领淘户外写真海报"))
  340. {
  341. entity.IsVerifyToSupplier = true;
  342. entity.SupplierId = 59;
  343. entity.FinishPlaceTime = DateTime.Now;
  344. }
  345. }
  346. if (entity.payment <= 500 && num > 0 && num <= 50)
  347. {
  348. //数量0-50
  349. if ((entity.seller_memo.Contains("桌布") && (entity.seller_memo.Contains("白底") || entity.seller_memo.Contains("蓝底") || entity.seller_memo.Contains("红底"))) || (entity.seller_memo.Contains("双喷") && entity.seller_memo.Contains("班旗") && entity.seller_memo.Contains("旗帜布") && (entity.seller_memo.Contains("左缝筒") || entity.seller_memo.Contains("四角打孔") || entity.seller_memo.Contains("净裁"))))
  350. {
  351. entity.IsVerifyToSupplier = true;
  352. entity.SupplierId = 90;
  353. entity.FinishPlaceTime = DateTime.Now;
  354. }
  355. }
  356. }
  357. if (entity.ProductId == 2690)
  358. {
  359. entity.IsVerifyToSupplier = true;
  360. entity.SupplierId = 97;
  361. entity.FinishPlaceTime = DateTime.Now;
  362. }
  363. if (entity.ProductId == 2701)
  364. {
  365. entity.IsVerifyToSupplier = true;
  366. entity.SupplierId = 98;
  367. entity.FinishPlaceTime = DateTime.Now;
  368. }
  369. string dPath = entity.FinishDesignTime.GetValueOrDefault().ToString("yyyyMMdd");
  370. //XLog.SaveLog(5, dPath);
  371. string sPath = Path.Combine(upPath, dPath);
  372. string cpath = Path.Combine(upPath, entity.FinishDesignTime.GetValueOrDefault().ToString("yyyyMM"));
  373. //XLog.SaveLog(5, sPath);
  374. //XLog.SaveLog(5, Directory.Exists(sPath).ToString());
  375. if (!Directory.Exists(sPath)) Directory.CreateDirectory(sPath);
  376. if (!Directory.Exists(cpath)) Directory.CreateDirectory(cpath);
  377. string saveFile = Path.Combine(sPath, file_name);
  378. string f_ext = Path.GetExtension(saveFile);
  379. string[] extArr = new string[] { ".cdr", ".zip", ".rar", ".pdf" };
  380. string _file = "";
  381. foreach (string ext in extArr)
  382. {
  383. if (f_ext != ext)
  384. {
  385. _file = saveFile.Replace(f_ext, ext);
  386. if (File.Exists(_file)) File.Delete(_file);
  387. }
  388. else
  389. {
  390. if (File.Exists(saveFile)) File.Delete(saveFile);
  391. }
  392. }
  393. //上传文件
  394. postFile.SaveAs(saveFile);
  395. entity.FileMd5 = GetMD5HashFromFile(postFile);
  396. //XLog.SaveLog(0, saveFile);
  397. entity.UpdateTime = DateTime.Now;
  398. entity.Update();
  399. //CeErpTradeCell.UpdateRelationOrder(entity);
  400. CeErpTradeCell.UpdateRelationOrder(entity.ctid);
  401. CeErpProduct product = CeErpProduct.GetById(entity.ProductId);
  402. string param = "[{ \"product_type\": \"" + product.PType + "\", \"remark\": \"" + entity.seller_memo + "\" }]";
  403. sysHttpClient(param);
  404. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, userId, "上传设计文件-" + saveFile);
  405. if (ctid.IndexOf("S_") >= -1)
  406. {
  407. StringBuilder sql = new StringBuilder();
  408. sql.AppendFormat("select * from Ce_ErpTradeCellExtend where ctid='{0}'", entity.ctid);
  409. DataTable cellEx = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  410. if (cellEx.Rows.Count > 0)
  411. {
  412. string txtReprintTime = cellEx.Rows[0]["ReprintTime"].ToString();
  413. if (!string.IsNullOrEmpty(txtReprintTime) && !"null".Equals(txtReprintTime))
  414. {
  415. DateTime reprint = DateTime.Parse(txtReprintTime);
  416. if (DateTime.Compare(DateTime.Now.AddHours(-24), reprint) >= 0)
  417. {
  418. string parentId = entity.ctid.Substring(2);
  419. string update_sql = string.Format("update Ce_ErpTradeAfterSaleExtend set ReprintOut = ReprintOut+1 where tid= '{0}' ;", parentId);
  420. CeErpTradeLog.ExecuteNonQuery(update_sql);
  421. }
  422. }
  423. }
  424. }
  425. if (Path.GetExtension(saveFile).IndexOf("cdr", StringComparison.OrdinalIgnoreCase) != -1)
  426. {
  427. //string sql = string.Format("insert into s_cdrtopng(name,addtime)values('{0}',getdate()) ;", saveFile);
  428. //CeErpTradeLog.ExecuteNonQuery(sql);
  429. }
  430. else if (Path.GetExtension(saveFile).IndexOf("zip", StringComparison.OrdinalIgnoreCase) != -1 || Path.GetExtension(saveFile).IndexOf("rar", StringComparison.OrdinalIgnoreCase) != -1)
  431. {
  432. try
  433. {
  434. DecompressZIPandRAR(saveFile, sPath, file_name);
  435. }
  436. catch (Exception ex)
  437. {
  438. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, entity.DesignUserId, "解压失败!");
  439. XLog.SaveLog(0, "上传解压发生错误!" + ex.Message);
  440. }
  441. //Decompress(saveFile,sPath);
  442. }
  443. //new Thread(new ThreadStart(delegate ()
  444. //{
  445. //System.Threading.Thread.Sleep(2000);
  446. if (Path.GetExtension(saveFile).IndexOf("cdr", StringComparison.OrdinalIgnoreCase) != -1)
  447. {
  448. var identity = WindowsIdentity.GetCurrent();
  449. var principal = new WindowsPrincipal(identity);
  450. Task.Run(async () =>
  451. {
  452. string filePath = Path.GetFullPath(saveFile);
  453. string targPath = Path.Combine(cpath, file_name);
  454. using (identity.Impersonate())
  455. {
  456. try
  457. {
  458. if (File.Exists(filePath))
  459. {
  460. cdrConvert.CdrConvertPng(filePath, targPath);
  461. }
  462. }
  463. catch (Exception ex)
  464. {
  465. XLog.SaveLog(0, filePath + ",转成图片出错:" + ex.Message);
  466. }
  467. }
  468. });
  469. }
  470. //})).Start();
  471. conSuc("上传成功!");
  472. return;
  473. }
  474. else
  475. {
  476. conErc("找不到对应的订单");
  477. return;
  478. }
  479. }
  480. catch (Exception ex)
  481. {
  482. conErc("上传发生错误!" + CommonHelper.FormatTextArea(ex.Message));
  483. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, entity.DesignUserId, "上传失败!");
  484. XLog.SaveLog(0, "上传发生错误!" + ex.Message);
  485. return;
  486. }
  487. finally
  488. {
  489. if (postFile != null)
  490. {
  491. postFile.InputStream.Close();
  492. }
  493. }
  494. }
  495. conErc("空文件!");
  496. }
  497. class CdrConvert
  498. {
  499. private object lockObject = new object();
  500. public void CdrConvertPng(string filePath, string targPath)
  501. {
  502. lock (lockObject)
  503. {
  504. targPath = targPath.Replace(".cdr", ".png");
  505. try
  506. {
  507. Application cdr = new Application();
  508. cdr.OpenDocument(filePath, 1);
  509. cdr.ActiveDocument.ExportBitmap(
  510. targPath,
  511. cdrFilter.cdrPNG,
  512. cdrExportRange.cdrCurrentPage,
  513. cdrImageType.cdrRGBColorImage,
  514. 0, 0, 72, 72,
  515. cdrAntiAliasingType.cdrNoAntiAliasing,
  516. false,
  517. true,
  518. true,
  519. false,
  520. cdrCompressionType.cdrCompressionNone,
  521. null).Finish();
  522. cdr.ActiveDocument.Close();
  523. cdr.Quit();
  524. cdr = null;
  525. }
  526. catch (Exception ex)
  527. {
  528. XLog.SaveLog(0, targPath + ",转成图片出错:" + ex.Message);
  529. }
  530. finally
  531. {
  532. KillProcessByName("CorelDRW");
  533. }
  534. }
  535. }
  536. void KillProcessByName(string processName)
  537. {
  538. Process[] processes = Process.GetProcessesByName(processName);
  539. foreach (Process process in processes)
  540. {
  541. try
  542. {
  543. process.Kill();
  544. process.WaitForExit(); // 等待进程退出
  545. }
  546. catch (Exception ex)
  547. {
  548. }
  549. }
  550. }
  551. }
  552. public static int getProductCount(string txt)
  553. {
  554. // 提取"个"或"张"前面的数字
  555. int unit = 0;
  556. try
  557. {
  558. string unitPattern = @"(\d+)(?=个|张|本|套|件|卷|劵|条|箱 )";
  559. Match unitMatch = Regex.Match(txt, unitPattern);
  560. string unitNum = unitMatch.Success ? unitMatch.Groups[1].Value : "1";
  561. if (!int.TryParse(unitNum, out unit))
  562. {
  563. return 0;
  564. }
  565. }
  566. catch (Exception ex)
  567. {
  568. }
  569. return unit;
  570. }
  571. private void DecompressZIPandRAR(string zipFile, string targetPath, string filename)
  572. {
  573. string notExtension = filename.Substring(0, filename.Length - 4);
  574. SevenZipExtractor.SetLibraryPath(Server.MapPath("bin\\7z.dll"));
  575. LibraryFeature lf = SevenZipExtractor.CurrentLibraryFeatures;
  576. using (SevenZipExtractor szExtra = new SevenZipExtractor(zipFile))
  577. {
  578. //szExtra.ExtractArchive("d:\\temp");
  579. foreach (string afn in szExtra.ArchiveFileNames)
  580. {
  581. if (afn.IndexOf(notExtension, StringComparison.OrdinalIgnoreCase) != -1)
  582. {
  583. szExtra.ExtractFiles(targetPath, afn);
  584. break;
  585. }
  586. }
  587. }
  588. }
  589. public void sysHttpClient(string param)
  590. {
  591. Task.Run(async () =>
  592. {
  593. var client = new HttpClient();
  594. var request = new HttpRequestMessage
  595. {
  596. Method = HttpMethod.Post,
  597. RequestUri = new Uri("http://47.122.57.102:11004/quotations/batch"),
  598. Content = new StringContent(param)
  599. {
  600. Headers =
  601. {
  602. ContentType = new MediaTypeHeaderValue("application/json")
  603. }
  604. }
  605. };
  606. var task = Task.Run(() =>
  607. {
  608. try
  609. {
  610. var response = client.SendAsync(request).Result;
  611. response.EnsureSuccessStatusCode();
  612. return response.Content.ReadAsStringAsync().Result;
  613. }
  614. catch (Exception e)
  615. {
  616. return "";
  617. }
  618. });
  619. });
  620. }
  621. private void Decompress(string GzipFile, string targetPath)
  622. {
  623. //string directoryName = Path.GetDirectoryName(targetPath + "\\") + "\\";
  624. string directoryName = targetPath + "\\";
  625. if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
  626. //helper.writeLog(GzipFile);
  627. //helper.writeLog(directoryName);
  628. string CurrentDirectory = directoryName;
  629. byte[] data = new byte[2048];
  630. int size = 2048;
  631. ZipEntry theEntry = null;
  632. Stream _stream = File.OpenRead(GzipFile);
  633. if (_stream.Length == 0) { _stream.Close(); return; }
  634. try
  635. {
  636. using (ZipInputStream s = new ZipInputStream(_stream))
  637. {
  638. while ((theEntry = s.GetNextEntry()) != null)
  639. {
  640. if (theEntry.IsDirectory)
  641. {// 该结点是目录
  642. if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
  643. }
  644. else
  645. {
  646. if (theEntry.Name != String.Empty && (theEntry.Name.IndexOf(".png", StringComparison.OrdinalIgnoreCase) != -1 || theEntry.Name.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) != -1))
  647. {
  648. //解压文件到指定的目录
  649. using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
  650. {
  651. while (true)
  652. {
  653. size = s.Read(data, 0, data.Length);
  654. if (size <= 0) break;
  655. streamWriter.Write(data, 0, size);
  656. }
  657. streamWriter.Close();
  658. }
  659. break;
  660. }
  661. }
  662. }
  663. s.Close();
  664. }
  665. }
  666. catch (Exception ex)
  667. {
  668. XLog.SaveLog(0, "解压uncau:" + ex.Message);
  669. }
  670. }
  671. public string unCompressRAR(string unRarPatch, string rarPatch, string rarName)
  672. {
  673. string the_rar;
  674. RegistryKey the_Reg;
  675. object the_Obj;
  676. string the_Info;
  677. try
  678. {
  679. the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
  680. the_Obj = the_Reg.GetValue("");
  681. the_rar = the_Obj.ToString();
  682. the_Reg.Close();
  683. if (Directory.Exists(unRarPatch) == false)
  684. {
  685. Directory.CreateDirectory(unRarPatch);
  686. }
  687. the_Info = "x " + rarName + " " + unRarPatch + " -y";
  688. ProcessStartInfo the_StartInfo = new ProcessStartInfo();
  689. the_StartInfo.FileName = the_rar;
  690. the_StartInfo.Arguments = the_Info;
  691. the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  692. the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径
  693. Process the_Process = new Process();
  694. the_Process.StartInfo = the_StartInfo;
  695. the_Process.Start();
  696. the_Process.WaitForExit();
  697. the_Process.Close();
  698. }
  699. catch (Exception ex)
  700. {
  701. throw ex;
  702. }
  703. return unRarPatch;
  704. }
  705. private void ShellCdrConvertPng(string path, string targPath)
  706. {
  707. ShellFile shellFile = ShellFile.FromFilePath(path);
  708. System.Drawing.Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
  709. //在画板的指定位置画图
  710. targPath = targPath.Replace(".cdr", ".png");
  711. shellThumb.Save(targPath, ImageFormat.Png);
  712. shellThumb.Dispose();
  713. shellFile.Dispose();
  714. }
  715. private void CdrConvertPng(string path, string targPath)
  716. {
  717. using (Aspose.Imaging.FileFormats.Cdr.CdrImage image = (Aspose.Imaging.FileFormats.Cdr.CdrImage)Aspose.Imaging.Image.Load(path))
  718. {
  719. PngOptions options = new Aspose.Imaging.ImageOptions.PngOptions();
  720. options.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
  721. // Set rasterization options for fileformat
  722. options.VectorRasterizationOptions = (Aspose.Imaging.ImageOptions.VectorRasterizationOptions)
  723. image.GetDefaultOptions(new object[] { Aspose.Imaging.Color.White, image.Width, image.Height });
  724. options.VectorRasterizationOptions.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  725. options.VectorRasterizationOptions.SmoothingMode = Aspose.Imaging.SmoothingMode.None;
  726. targPath = targPath.Replace(".cdr", ".png");
  727. image.Save(targPath, options);
  728. options.Dispose();
  729. }
  730. }
  731. private string CdrExPng(string path, string targPath)
  732. {
  733. try
  734. {
  735. targPath = targPath.Replace(".cdr", ".png");
  736. Application cdr = new Application();
  737. cdr.OpenDocument(path, 1);
  738. cdr.ActiveDocument.ExportBitmap(
  739. targPath,
  740. cdrFilter.cdrPNG,
  741. cdrExportRange.cdrCurrentPage,
  742. cdrImageType.cdrRGBColorImage,
  743. 0, 0, 72, 72,
  744. cdrAntiAliasingType.cdrNoAntiAliasing,
  745. false,
  746. true,
  747. true,
  748. false,
  749. cdrCompressionType.cdrCompressionNone,
  750. null).Finish();
  751. cdr.ActiveDocument.Close();
  752. cdr.Quit();
  753. cdr = null;
  754. GC.Collect();
  755. }
  756. catch (Exception ex)
  757. {
  758. XLog.SaveLog(0, path + ",转成图片出错:" + ex.Message);
  759. }
  760. finally
  761. {
  762. }
  763. return "111";
  764. }
  765. private static object cdrpngobj = new object();
  766. private void CdrExportPng(string path, string cdrFile)
  767. {
  768. lock (cdrpngobj)
  769. {
  770. string fname = curPath + "\\" + Path.GetFileNameWithoutExtension(cdrFile) + ".png";
  771. string new_fname = path + "\\" + Path.GetFileNameWithoutExtension(cdrFile) + ".png";
  772. CorelDRAW.Application cdr = new CorelDRAW.Application();
  773. cdr.OpenDocument(cdrFile, 1);
  774. cdr.ActiveDocument.ExportBitmap(
  775. fname,
  776. CorelDRAW.cdrFilter.cdrPNG,
  777. CorelDRAW.cdrExportRange.cdrCurrentPage,
  778. CorelDRAW.cdrImageType.cdrRGBColorImage,
  779. 0, 0, 72, 72,
  780. CorelDRAW.cdrAntiAliasingType.cdrNoAntiAliasing,
  781. false,
  782. true,
  783. true,
  784. false,
  785. CorelDRAW.cdrCompressionType.cdrCompressionNone,
  786. null).Finish();
  787. cdr.ActiveDocument.Close();
  788. cdr.Quit();
  789. if (File.Exists(fname))
  790. {
  791. File.Copy(fname, new_fname);
  792. File.Delete(fname);
  793. }
  794. }
  795. }
  796. public static string MidStrEx(string sourse, string startstr, string endstr)
  797. {
  798. string result = string.Empty;
  799. int startindex, endindex;
  800. try
  801. {
  802. startindex = sourse.IndexOf(startstr);
  803. if (startindex == -1)
  804. return result;
  805. string tmpstr = sourse.Substring(startindex + startstr.Length);
  806. endindex = tmpstr.IndexOf(endstr);
  807. if (endindex == -1)
  808. return result;
  809. result = tmpstr.Remove(endindex);
  810. }
  811. catch (Exception ex)
  812. {
  813. Console.WriteLine("MidStrEx Err:" + ex.Message);
  814. }
  815. return result;
  816. }
  817. private static string logPath = ConfigurationManager.AppSettings["curPath"] + "\\log";
  818. private static object logFlag = new object();
  819. public static void writeLog(string log)
  820. {
  821. lock (logFlag)
  822. {
  823. using (FileStream fileStream = new FileStream(logPath + "\\" + DateTime.Now.ToString("yy-MM-dd") + ".log", FileMode.Append, FileAccess.Write))
  824. {
  825. using (StreamWriter sw = new StreamWriter(fileStream, Encoding.Default))
  826. {
  827. sw.Write(log + " ------时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
  828. sw.Flush();
  829. }
  830. }
  831. }
  832. }
  833. }