uploadFile.aspx.cs 35 KB

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