uploadFile.aspx.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. if (product != null)
  403. {
  404. string param = "[{ \"product_type\": \"" + product.PType + "\",\"remark\": \"" + entity.seller_memo + "\",\"index\": \"" + entity.OrderSn + "\",\"orderid\": \"" + entity.ctid + "\",\"price\": \"" + entity.payment + "\"}]";
  405. sysHttpClient(param);
  406. }
  407. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, userId, "上传设计文件-" + saveFile);
  408. if (ctid.IndexOf("S_") >= -1)
  409. {
  410. StringBuilder sql = new StringBuilder();
  411. sql.AppendFormat("select * from Ce_ErpTradeCellExtend where ctid='{0}'", entity.ctid);
  412. DataTable cellEx = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  413. if (cellEx.Rows.Count > 0)
  414. {
  415. string txtReprintTime = cellEx.Rows[0]["ReprintTime"].ToString();
  416. if (!string.IsNullOrEmpty(txtReprintTime) && !"null".Equals(txtReprintTime))
  417. {
  418. DateTime reprint = DateTime.Parse(txtReprintTime);
  419. if (DateTime.Compare(DateTime.Now.AddHours(-24), reprint) >= 0)
  420. {
  421. string parentId = entity.ctid.Substring(2);
  422. string update_sql = string.Format("update Ce_ErpTradeAfterSaleExtend set ReprintOut = ReprintOut+1 where tid= '{0}' ;", parentId);
  423. CeErpTradeLog.ExecuteNonQuery(update_sql);
  424. }
  425. }
  426. }
  427. }
  428. if (Path.GetExtension(saveFile).IndexOf("cdr", StringComparison.OrdinalIgnoreCase) != -1)
  429. {
  430. //string sql = string.Format("insert into s_cdrtopng(name,addtime)values('{0}',getdate()) ;", saveFile);
  431. //CeErpTradeLog.ExecuteNonQuery(sql);
  432. }
  433. else if (Path.GetExtension(saveFile).IndexOf("zip", StringComparison.OrdinalIgnoreCase) != -1 || Path.GetExtension(saveFile).IndexOf("rar", StringComparison.OrdinalIgnoreCase) != -1)
  434. {
  435. try
  436. {
  437. DecompressZIPandRAR(saveFile, sPath, file_name);
  438. }
  439. catch (Exception ex)
  440. {
  441. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, entity.DesignUserId, "解压失败!");
  442. XLog.SaveLog(0, "上传解压发生错误!" + ex.Message);
  443. }
  444. //Decompress(saveFile,sPath);
  445. }
  446. //new Thread(new ThreadStart(delegate ()
  447. //{
  448. //System.Threading.Thread.Sleep(2000);
  449. if (Path.GetExtension(saveFile).IndexOf("cdr", StringComparison.OrdinalIgnoreCase) != -1)
  450. {
  451. var identity = WindowsIdentity.GetCurrent();
  452. var principal = new WindowsPrincipal(identity);
  453. Task.Run(async () =>
  454. {
  455. string filePath = Path.GetFullPath(saveFile);
  456. string targPath = Path.Combine(cpath, file_name);
  457. using (identity.Impersonate())
  458. {
  459. try
  460. {
  461. if (File.Exists(filePath))
  462. {
  463. cdrConvert.CdrConvertPng(filePath, targPath);
  464. }
  465. }
  466. catch (Exception ex)
  467. {
  468. XLog.SaveLog(0, filePath + ",转成图片出错:" + ex.Message);
  469. }
  470. }
  471. });
  472. }
  473. //})).Start();
  474. conSuc("上传成功!");
  475. return;
  476. }
  477. else
  478. {
  479. conErc("找不到对应的订单");
  480. return;
  481. }
  482. }
  483. catch (Exception ex)
  484. {
  485. conErc("上传发生错误!" + CommonHelper.FormatTextArea(ex.Message));
  486. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, entity.DesignUserId, "上传失败!");
  487. XLog.SaveLog(0, "上传发生错误!" + ex.Message);
  488. return;
  489. }
  490. finally
  491. {
  492. if (postFile != null)
  493. {
  494. postFile.InputStream.Close();
  495. }
  496. }
  497. }
  498. conErc("空文件!");
  499. }
  500. class CdrConvert
  501. {
  502. private object lockObject = new object();
  503. public void CdrConvertPng(string filePath, string targPath)
  504. {
  505. lock (lockObject)
  506. {
  507. targPath = targPath.Replace(".cdr", ".png");
  508. try
  509. {
  510. Application cdr = new Application();
  511. cdr.OpenDocument(filePath, 1);
  512. cdr.ActiveDocument.ExportBitmap(
  513. targPath,
  514. cdrFilter.cdrPNG,
  515. cdrExportRange.cdrCurrentPage,
  516. cdrImageType.cdrRGBColorImage,
  517. 0, 0, 72, 72,
  518. cdrAntiAliasingType.cdrNoAntiAliasing,
  519. false,
  520. true,
  521. true,
  522. false,
  523. cdrCompressionType.cdrCompressionNone,
  524. null).Finish();
  525. cdr.ActiveDocument.Close();
  526. cdr.Quit();
  527. cdr = null;
  528. }
  529. catch (Exception ex)
  530. {
  531. XLog.SaveLog(0, targPath + ",转成图片出错:" + ex.Message);
  532. }
  533. finally
  534. {
  535. KillProcessByName("CorelDRW");
  536. }
  537. }
  538. }
  539. void KillProcessByName(string processName)
  540. {
  541. Process[] processes = Process.GetProcessesByName(processName);
  542. foreach (Process process in processes)
  543. {
  544. try
  545. {
  546. process.Kill();
  547. process.WaitForExit(); // 等待进程退出
  548. }
  549. catch (Exception ex)
  550. {
  551. }
  552. }
  553. }
  554. }
  555. public static int getProductCount(string txt)
  556. {
  557. // 提取"个"或"张"前面的数字
  558. int unit = 0;
  559. try
  560. {
  561. string unitPattern = @"(\d+)(?=个|张|本|套|件|卷|劵|条|箱 )";
  562. Match unitMatch = Regex.Match(txt, unitPattern);
  563. string unitNum = unitMatch.Success ? unitMatch.Groups[1].Value : "1";
  564. if (!int.TryParse(unitNum, out unit))
  565. {
  566. return 0;
  567. }
  568. }
  569. catch (Exception ex)
  570. {
  571. }
  572. return unit;
  573. }
  574. private void DecompressZIPandRAR(string zipFile, string targetPath, string filename)
  575. {
  576. string notExtension = filename.Substring(0, filename.Length - 4);
  577. SevenZipExtractor.SetLibraryPath(Server.MapPath("bin\\7z.dll"));
  578. LibraryFeature lf = SevenZipExtractor.CurrentLibraryFeatures;
  579. using (SevenZipExtractor szExtra = new SevenZipExtractor(zipFile))
  580. {
  581. //szExtra.ExtractArchive("d:\\temp");
  582. foreach (string afn in szExtra.ArchiveFileNames)
  583. {
  584. if (afn.IndexOf(notExtension, StringComparison.OrdinalIgnoreCase) != -1)
  585. {
  586. szExtra.ExtractFiles(targetPath, afn);
  587. break;
  588. }
  589. }
  590. }
  591. }
  592. public void sysHttpClient(string param)
  593. {
  594. Task.Run(async () =>
  595. {
  596. var client = new HttpClient();
  597. var request = new HttpRequestMessage
  598. {
  599. Method = HttpMethod.Post,
  600. RequestUri = new Uri("http://47.122.57.102:11004/quotations/batch"),
  601. Content = new StringContent(param)
  602. {
  603. Headers =
  604. {
  605. ContentType = new MediaTypeHeaderValue("application/json")
  606. }
  607. }
  608. };
  609. var task = Task.Run(() =>
  610. {
  611. try
  612. {
  613. var response = client.SendAsync(request).Result;
  614. response.EnsureSuccessStatusCode();
  615. writeLog(param + "-" + response.Content.ReadAsStringAsync().Result);
  616. return response.Content.ReadAsStringAsync().Result;
  617. }
  618. catch (Exception e)
  619. {
  620. return "";
  621. }
  622. });
  623. });
  624. }
  625. private void Decompress(string GzipFile, string targetPath)
  626. {
  627. //string directoryName = Path.GetDirectoryName(targetPath + "\\") + "\\";
  628. string directoryName = targetPath + "\\";
  629. if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
  630. //helper.writeLog(GzipFile);
  631. //helper.writeLog(directoryName);
  632. string CurrentDirectory = directoryName;
  633. byte[] data = new byte[2048];
  634. int size = 2048;
  635. ZipEntry theEntry = null;
  636. Stream _stream = File.OpenRead(GzipFile);
  637. if (_stream.Length == 0) { _stream.Close(); return; }
  638. try
  639. {
  640. using (ZipInputStream s = new ZipInputStream(_stream))
  641. {
  642. while ((theEntry = s.GetNextEntry()) != null)
  643. {
  644. if (theEntry.IsDirectory)
  645. {// 该结点是目录
  646. if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
  647. }
  648. else
  649. {
  650. if (theEntry.Name != String.Empty && (theEntry.Name.IndexOf(".png", StringComparison.OrdinalIgnoreCase) != -1 || theEntry.Name.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) != -1))
  651. {
  652. //解压文件到指定的目录
  653. using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
  654. {
  655. while (true)
  656. {
  657. size = s.Read(data, 0, data.Length);
  658. if (size <= 0) break;
  659. streamWriter.Write(data, 0, size);
  660. }
  661. streamWriter.Close();
  662. }
  663. break;
  664. }
  665. }
  666. }
  667. s.Close();
  668. }
  669. }
  670. catch (Exception ex)
  671. {
  672. XLog.SaveLog(0, "解压uncau:" + ex.Message);
  673. }
  674. }
  675. public string unCompressRAR(string unRarPatch, string rarPatch, string rarName)
  676. {
  677. string the_rar;
  678. RegistryKey the_Reg;
  679. object the_Obj;
  680. string the_Info;
  681. try
  682. {
  683. the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
  684. the_Obj = the_Reg.GetValue("");
  685. the_rar = the_Obj.ToString();
  686. the_Reg.Close();
  687. if (Directory.Exists(unRarPatch) == false)
  688. {
  689. Directory.CreateDirectory(unRarPatch);
  690. }
  691. the_Info = "x " + rarName + " " + unRarPatch + " -y";
  692. ProcessStartInfo the_StartInfo = new ProcessStartInfo();
  693. the_StartInfo.FileName = the_rar;
  694. the_StartInfo.Arguments = the_Info;
  695. the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  696. the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径
  697. Process the_Process = new Process();
  698. the_Process.StartInfo = the_StartInfo;
  699. the_Process.Start();
  700. the_Process.WaitForExit();
  701. the_Process.Close();
  702. }
  703. catch (Exception ex)
  704. {
  705. throw ex;
  706. }
  707. return unRarPatch;
  708. }
  709. private void ShellCdrConvertPng(string path, string targPath)
  710. {
  711. ShellFile shellFile = ShellFile.FromFilePath(path);
  712. System.Drawing.Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
  713. //在画板的指定位置画图
  714. targPath = targPath.Replace(".cdr", ".png");
  715. shellThumb.Save(targPath, ImageFormat.Png);
  716. shellThumb.Dispose();
  717. shellFile.Dispose();
  718. }
  719. private void CdrConvertPng(string path, string targPath)
  720. {
  721. using (Aspose.Imaging.FileFormats.Cdr.CdrImage image = (Aspose.Imaging.FileFormats.Cdr.CdrImage)Aspose.Imaging.Image.Load(path))
  722. {
  723. PngOptions options = new Aspose.Imaging.ImageOptions.PngOptions();
  724. options.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
  725. // Set rasterization options for fileformat
  726. options.VectorRasterizationOptions = (Aspose.Imaging.ImageOptions.VectorRasterizationOptions)
  727. image.GetDefaultOptions(new object[] { Aspose.Imaging.Color.White, image.Width, image.Height });
  728. options.VectorRasterizationOptions.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  729. options.VectorRasterizationOptions.SmoothingMode = Aspose.Imaging.SmoothingMode.None;
  730. targPath = targPath.Replace(".cdr", ".png");
  731. image.Save(targPath, options);
  732. options.Dispose();
  733. }
  734. }
  735. private string CdrExPng(string path, string targPath)
  736. {
  737. try
  738. {
  739. targPath = targPath.Replace(".cdr", ".png");
  740. Application cdr = new Application();
  741. cdr.OpenDocument(path, 1);
  742. cdr.ActiveDocument.ExportBitmap(
  743. targPath,
  744. cdrFilter.cdrPNG,
  745. cdrExportRange.cdrCurrentPage,
  746. cdrImageType.cdrRGBColorImage,
  747. 0, 0, 72, 72,
  748. cdrAntiAliasingType.cdrNoAntiAliasing,
  749. false,
  750. true,
  751. true,
  752. false,
  753. cdrCompressionType.cdrCompressionNone,
  754. null).Finish();
  755. cdr.ActiveDocument.Close();
  756. cdr.Quit();
  757. cdr = null;
  758. GC.Collect();
  759. }
  760. catch (Exception ex)
  761. {
  762. XLog.SaveLog(0, path + ",转成图片出错:" + ex.Message);
  763. }
  764. finally
  765. {
  766. }
  767. return "111";
  768. }
  769. private static object cdrpngobj = new object();
  770. private void CdrExportPng(string path, string cdrFile)
  771. {
  772. lock (cdrpngobj)
  773. {
  774. string fname = curPath + "\\" + Path.GetFileNameWithoutExtension(cdrFile) + ".png";
  775. string new_fname = path + "\\" + Path.GetFileNameWithoutExtension(cdrFile) + ".png";
  776. CorelDRAW.Application cdr = new CorelDRAW.Application();
  777. cdr.OpenDocument(cdrFile, 1);
  778. cdr.ActiveDocument.ExportBitmap(
  779. fname,
  780. CorelDRAW.cdrFilter.cdrPNG,
  781. CorelDRAW.cdrExportRange.cdrCurrentPage,
  782. CorelDRAW.cdrImageType.cdrRGBColorImage,
  783. 0, 0, 72, 72,
  784. CorelDRAW.cdrAntiAliasingType.cdrNoAntiAliasing,
  785. false,
  786. true,
  787. true,
  788. false,
  789. CorelDRAW.cdrCompressionType.cdrCompressionNone,
  790. null).Finish();
  791. cdr.ActiveDocument.Close();
  792. cdr.Quit();
  793. if (File.Exists(fname))
  794. {
  795. File.Copy(fname, new_fname);
  796. File.Delete(fname);
  797. }
  798. }
  799. }
  800. public static string MidStrEx(string sourse, string startstr, string endstr)
  801. {
  802. string result = string.Empty;
  803. int startindex, endindex;
  804. try
  805. {
  806. startindex = sourse.IndexOf(startstr);
  807. if (startindex == -1)
  808. return result;
  809. string tmpstr = sourse.Substring(startindex + startstr.Length);
  810. endindex = tmpstr.IndexOf(endstr);
  811. if (endindex == -1)
  812. return result;
  813. result = tmpstr.Remove(endindex);
  814. }
  815. catch (Exception ex)
  816. {
  817. Console.WriteLine("MidStrEx Err:" + ex.Message);
  818. }
  819. return result;
  820. }
  821. private static string logPath = ConfigurationManager.AppSettings["curPath"] + "\\log";
  822. private static object logFlag = new object();
  823. public static void writeLog(string log)
  824. {
  825. lock (logFlag)
  826. {
  827. using (FileStream fileStream = new FileStream(logPath + "\\" + DateTime.Now.ToString("yy-MM-dd") + ".log", FileMode.Append, FileAccess.Write))
  828. {
  829. using (StreamWriter sw = new StreamWriter(fileStream, Encoding.Default))
  830. {
  831. sw.Write(log + " ------时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
  832. sw.Flush();
  833. }
  834. }
  835. }
  836. }
  837. }