uploadFile.aspx.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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.Threading;
  21. using System.Runtime.InteropServices;
  22. using System.Data;
  23. using System.Text.RegularExpressions;
  24. using NHibernate.Mapping;
  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 (System.Text.RegularExpressions.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. if (entity.payment <= 300 && num >= 200 && num <= 3000 && entity.ShopId != 14 && entity.ShopId != 99)
  253. {
  254. //300克铜板纸/铜版纸300克-覆哑膜/覆膜/不覆膜-直角/裁切
  255. if (!entity.seller_memo.Contains("opp") && !entity.seller_memo.Contains("按文件") && !entity.seller_memo.Contains("流苏"))
  256. {
  257. 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("不覆膜")))
  258. {
  259. entity.IsVerifyToSupplier = true;
  260. entity.SupplierId = 14;
  261. entity.FinishPlaceTime = DateTime.Now;
  262. }
  263. }
  264. }
  265. if (entity.payment <= 100 && num > 200 && num <= 1500)
  266. {
  267. //数量0-1500
  268. //0-80*54
  269. string pattern = @"\b(\d{1,5}[x\*]\d{1,5}(mm|cm))\b";
  270. string memo = entity.seller_memo.Replace("MM", "mm").Replace("CM", "cm");
  271. Regex reg = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline, TimeSpan.FromSeconds(2));//2秒后超时
  272. MatchCollection matches = reg.Matches(memo);//设定要查找的字符串
  273. string size = "";
  274. double min_width = 80;
  275. double min_height = 54;
  276. double width = 0;
  277. double height = 0;
  278. bool isIn = false;
  279. try
  280. {
  281. if (matches[0].Success)
  282. {
  283. size = matches[0].Groups[0].Value;
  284. size = size.Replace("mm", "");
  285. size = size.Replace("cm", "");
  286. string[] size_list = size.Split('x');
  287. if (size_list.Length > 1)
  288. {
  289. width = Convert.ToDouble(size_list[0]);
  290. height = Convert.ToDouble(size_list[1]);
  291. }
  292. if ((width <= min_width && height <= min_height) || (height <= min_width && width <= min_height))
  293. {
  294. isIn = true;
  295. }
  296. }
  297. }
  298. catch (Exception ex)
  299. {
  300. }
  301. //铜版纸不干胶-覆亮膜
  302. if (isIn && entity.seller_memo.Contains("铜版纸不干胶") && entity.seller_memo.Contains("覆亮膜") && !entity.seller_memo.Contains("烫金") && !entity.seller_memo.Contains("排序"))
  303. {
  304. entity.IsVerifyToSupplier = true;
  305. entity.SupplierId = 64;
  306. entity.FinishPlaceTime = DateTime.Now;
  307. }
  308. }
  309. if (entity.payment <= 500 && num > 200 && num <= 5000)
  310. {
  311. //数量0-5000
  312. if (entity.seller_memo.Contains("透明不干胶"))
  313. {
  314. entity.IsVerifyToSupplier = true;
  315. entity.SupplierId = 64;
  316. entity.FinishPlaceTime = DateTime.Now;
  317. }
  318. }
  319. if (entity.payment <= 100 && num > 200 && num <= 1000)
  320. {
  321. //数量0-1000
  322. if (entity.seller_memo.Contains("铜版纸不干胶") && entity.seller_memo.Contains("覆哑膜"))
  323. {
  324. entity.IsVerifyToSupplier = true;
  325. entity.SupplierId = 64;
  326. entity.FinishPlaceTime = DateTime.Now;
  327. }
  328. }
  329. if (entity.payment <= 500 && num > 0 && num <= 50)
  330. {
  331. //数量0-50
  332. if (entity.seller_memo.Contains("领淘550灯布海报") || entity.seller_memo.Contains("领淘户外写真海报"))
  333. {
  334. entity.IsVerifyToSupplier = true;
  335. entity.SupplierId = 59;
  336. entity.FinishPlaceTime = DateTime.Now;
  337. }
  338. }
  339. if (entity.payment <= 500 && num > 0 && num <= 50)
  340. {
  341. //数量0-50
  342. 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("净裁"))))
  343. {
  344. entity.IsVerifyToSupplier = true;
  345. entity.SupplierId = 90;
  346. entity.FinishPlaceTime = DateTime.Now;
  347. }
  348. }
  349. if (entity.ProductId == 2690)
  350. {
  351. entity.IsVerifyToSupplier = true;
  352. entity.SupplierId = 97;
  353. entity.FinishPlaceTime = DateTime.Now;
  354. }
  355. if (entity.ProductId == 2701)
  356. {
  357. entity.IsVerifyToSupplier = true;
  358. entity.SupplierId = 98;
  359. entity.FinishPlaceTime = DateTime.Now;
  360. }
  361. string dPath = entity.FinishDesignTime.GetValueOrDefault().ToString("yyyyMMdd");
  362. //XLog.SaveLog(5, dPath);
  363. string sPath = Path.Combine(upPath, dPath);
  364. string cpath = Path.Combine(upPath, entity.FinishDesignTime.GetValueOrDefault().ToString("yyyyMM"));
  365. //XLog.SaveLog(5, sPath);
  366. //XLog.SaveLog(5, Directory.Exists(sPath).ToString());
  367. if (!Directory.Exists(sPath)) Directory.CreateDirectory(sPath);
  368. if (!Directory.Exists(cpath)) Directory.CreateDirectory(cpath);
  369. string saveFile = Path.Combine(sPath, file_name);
  370. string f_ext = Path.GetExtension(saveFile);
  371. string[] extArr = new string[] { ".cdr", ".zip", ".rar", ".pdf" };
  372. string _file = "";
  373. foreach (string ext in extArr)
  374. {
  375. if (f_ext != ext)
  376. {
  377. _file = saveFile.Replace(f_ext, ext);
  378. if (File.Exists(_file)) File.Delete(_file);
  379. }
  380. else
  381. {
  382. if (File.Exists(saveFile)) File.Delete(saveFile);
  383. }
  384. }
  385. //上传文件
  386. postFile.SaveAs(saveFile);
  387. entity.FileMd5 = GetMD5HashFromFile(postFile);
  388. //XLog.SaveLog(0, saveFile);
  389. entity.UpdateTime = DateTime.Now;
  390. entity.Update();
  391. //CeErpTradeCell.UpdateRelationOrder(entity);
  392. CeErpTradeCell.UpdateRelationOrder(entity.ctid);
  393. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, userId, "上传设计文件-" + saveFile);
  394. if (ctid.IndexOf("S_") >= -1)
  395. {
  396. StringBuilder sql = new StringBuilder();
  397. sql.AppendFormat("select * from Ce_ErpTradeCellExtend where ctid='{0}'", entity.ctid);
  398. DataTable cellEx = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  399. if (cellEx.Rows.Count > 0)
  400. {
  401. string txtReprintTime = cellEx.Rows[0]["ReprintTime"].ToString();
  402. if (!string.IsNullOrEmpty(txtReprintTime) && !"null".Equals(txtReprintTime))
  403. {
  404. DateTime reprint = DateTime.Parse(txtReprintTime);
  405. if (DateTime.Compare(DateTime.Now.AddHours(-24), reprint) >= 0)
  406. {
  407. string parentId = entity.ctid.Substring(2);
  408. string update_sql = string.Format("update Ce_ErpTradeAfterSaleExtend set ReprintOut = ReprintOut+1 where tid= '{0}' ;", parentId);
  409. CeErpTradeLog.ExecuteNonQuery(update_sql);
  410. }
  411. }
  412. }
  413. }
  414. if (Path.GetExtension(saveFile).IndexOf("cdr", StringComparison.OrdinalIgnoreCase) != -1)
  415. {
  416. //string sql = string.Format("insert into s_cdrtopng(name,addtime)values('{0}',getdate()) ;", saveFile);
  417. //CeErpTradeLog.ExecuteNonQuery(sql);
  418. }
  419. else if (Path.GetExtension(saveFile).IndexOf("zip", StringComparison.OrdinalIgnoreCase) != -1 || Path.GetExtension(saveFile).IndexOf("rar", StringComparison.OrdinalIgnoreCase) != -1)
  420. {
  421. try
  422. {
  423. DecompressZIPandRAR(saveFile, sPath, file_name);
  424. }
  425. catch (Exception ex)
  426. {
  427. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, entity.DesignUserId, "解压失败!");
  428. XLog.SaveLog(0, "上传解压发生错误!" + ex.Message);
  429. }
  430. //Decompress(saveFile,sPath);
  431. }
  432. //new Thread(new ThreadStart(delegate ()
  433. //{
  434. //System.Threading.Thread.Sleep(2000);
  435. if (Path.GetExtension(saveFile).IndexOf("cdr", StringComparison.OrdinalIgnoreCase) != -1)
  436. {
  437. var identity = WindowsIdentity.GetCurrent();
  438. var principal = new WindowsPrincipal(identity);
  439. writeLog("上传完成!" + saveFile);
  440. Task.Run(async () =>
  441. {
  442. string filePath = Path.GetFullPath(saveFile);
  443. string targPath = Path.Combine(cpath, file_name);
  444. using (identity.Impersonate())
  445. {
  446. try
  447. {
  448. if (File.Exists(filePath))
  449. {
  450. cdrConvert.CdrConvertPng(filePath, targPath);
  451. }
  452. }
  453. catch (Exception ex)
  454. {
  455. XLog.SaveLog(0, filePath + ",转成图片出错:" + ex.Message);
  456. }
  457. }
  458. });
  459. }
  460. //})).Start();
  461. conSuc("上传成功!");
  462. return;
  463. }
  464. else
  465. {
  466. conErc("找不到对应的订单");
  467. return;
  468. }
  469. }
  470. catch (Exception ex)
  471. {
  472. conErc("上传发生错误!" + CommonHelper.FormatTextArea(ex.Message));
  473. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, entity.DesignUserId, "上传失败!");
  474. XLog.SaveLog(0, "上传发生错误!" + ex.Message);
  475. return;
  476. }
  477. finally
  478. {
  479. if (postFile != null)
  480. {
  481. postFile.InputStream.Close();
  482. }
  483. }
  484. }
  485. conErc("空文件!");
  486. }
  487. class CdrConvert
  488. {
  489. private object lockObject = new object();
  490. public void CdrConvertPng(string filePath, string targPath)
  491. {
  492. lock (lockObject)
  493. {
  494. writeLog("开始截图!" + filePath);
  495. targPath = targPath.Replace(".cdr", ".png");
  496. try
  497. {
  498. Application cdr = new Application();
  499. cdr.OpenDocument(filePath, 1);
  500. cdr.ActiveDocument.ExportBitmap(
  501. targPath,
  502. cdrFilter.cdrPNG,
  503. cdrExportRange.cdrCurrentPage,
  504. cdrImageType.cdrRGBColorImage,
  505. 0, 0, 72, 72,
  506. cdrAntiAliasingType.cdrNoAntiAliasing,
  507. false,
  508. true,
  509. true,
  510. false,
  511. cdrCompressionType.cdrCompressionNone,
  512. null).Finish();
  513. cdr.ActiveDocument.Close();
  514. cdr.Quit();
  515. cdr = null;
  516. }
  517. catch (Exception ex)
  518. {
  519. XLog.SaveLog(0, targPath + ",转成图片出错:" + ex.Message);
  520. }
  521. finally
  522. {
  523. KillProcessByName("CorelDRW");
  524. }
  525. }
  526. }
  527. void KillProcessByName(string processName)
  528. {
  529. Process[] processes = Process.GetProcessesByName(processName);
  530. foreach (Process process in processes)
  531. {
  532. try
  533. {
  534. process.Kill();
  535. process.WaitForExit(); // 等待进程退出
  536. }
  537. catch (Exception ex)
  538. {
  539. }
  540. }
  541. }
  542. }
  543. public static int getProductCount(string txt)
  544. {
  545. // 提取"个"或"张"前面的数字
  546. int unit = 0;
  547. try
  548. {
  549. string unitPattern = @"(\d+)(?=个|张|本|套|件|卷|劵|条|箱 )";
  550. Match unitMatch = Regex.Match(txt, unitPattern);
  551. string unitNum = unitMatch.Success ? unitMatch.Groups[1].Value : "1";
  552. if (!int.TryParse(unitNum, out unit))
  553. {
  554. return 0;
  555. }
  556. }
  557. catch (Exception ex)
  558. {
  559. }
  560. return unit;
  561. }
  562. private void DecompressZIPandRAR(string zipFile, string targetPath, string filename)
  563. {
  564. string notExtension = filename.Substring(0, filename.Length - 4);
  565. SevenZipExtractor.SetLibraryPath(Server.MapPath("bin\\7z.dll"));
  566. LibraryFeature lf = SevenZipExtractor.CurrentLibraryFeatures;
  567. using (SevenZipExtractor szExtra = new SevenZipExtractor(zipFile))
  568. {
  569. //szExtra.ExtractArchive("d:\\temp");
  570. foreach (string afn in szExtra.ArchiveFileNames)
  571. {
  572. if (afn.IndexOf(notExtension, StringComparison.OrdinalIgnoreCase) != -1)
  573. {
  574. szExtra.ExtractFiles(targetPath, afn);
  575. break;
  576. }
  577. }
  578. }
  579. }
  580. private void Decompress(string GzipFile, string targetPath)
  581. {
  582. //string directoryName = Path.GetDirectoryName(targetPath + "\\") + "\\";
  583. string directoryName = targetPath + "\\";
  584. if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
  585. //helper.writeLog(GzipFile);
  586. //helper.writeLog(directoryName);
  587. string CurrentDirectory = directoryName;
  588. byte[] data = new byte[2048];
  589. int size = 2048;
  590. ZipEntry theEntry = null;
  591. Stream _stream = File.OpenRead(GzipFile);
  592. if (_stream.Length == 0) { _stream.Close(); return; }
  593. try
  594. {
  595. using (ZipInputStream s = new ZipInputStream(_stream))
  596. {
  597. while ((theEntry = s.GetNextEntry()) != null)
  598. {
  599. if (theEntry.IsDirectory)
  600. {// 该结点是目录
  601. if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
  602. }
  603. else
  604. {
  605. if (theEntry.Name != String.Empty && (theEntry.Name.IndexOf(".png", StringComparison.OrdinalIgnoreCase) != -1 || theEntry.Name.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) != -1))
  606. {
  607. //解压文件到指定的目录
  608. using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
  609. {
  610. while (true)
  611. {
  612. size = s.Read(data, 0, data.Length);
  613. if (size <= 0) break;
  614. streamWriter.Write(data, 0, size);
  615. }
  616. streamWriter.Close();
  617. }
  618. break;
  619. }
  620. }
  621. }
  622. s.Close();
  623. }
  624. }
  625. catch (Exception ex)
  626. {
  627. XLog.SaveLog(0, "解压uncau:" + ex.Message);
  628. }
  629. }
  630. public string unCompressRAR(string unRarPatch, string rarPatch, string rarName)
  631. {
  632. string the_rar;
  633. RegistryKey the_Reg;
  634. object the_Obj;
  635. string the_Info;
  636. try
  637. {
  638. the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
  639. the_Obj = the_Reg.GetValue("");
  640. the_rar = the_Obj.ToString();
  641. the_Reg.Close();
  642. if (Directory.Exists(unRarPatch) == false)
  643. {
  644. Directory.CreateDirectory(unRarPatch);
  645. }
  646. the_Info = "x " + rarName + " " + unRarPatch + " -y";
  647. ProcessStartInfo the_StartInfo = new ProcessStartInfo();
  648. the_StartInfo.FileName = the_rar;
  649. the_StartInfo.Arguments = the_Info;
  650. the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  651. the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径
  652. Process the_Process = new Process();
  653. the_Process.StartInfo = the_StartInfo;
  654. the_Process.Start();
  655. the_Process.WaitForExit();
  656. the_Process.Close();
  657. }
  658. catch (Exception ex)
  659. {
  660. throw ex;
  661. }
  662. return unRarPatch;
  663. }
  664. private void ShellCdrConvertPng(string path, string targPath)
  665. {
  666. ShellFile shellFile = ShellFile.FromFilePath(path);
  667. System.Drawing.Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
  668. //在画板的指定位置画图
  669. targPath = targPath.Replace(".cdr", ".png");
  670. shellThumb.Save(targPath, ImageFormat.Png);
  671. shellThumb.Dispose();
  672. shellFile.Dispose();
  673. }
  674. private void CdrConvertPng(string path, string targPath)
  675. {
  676. using (Aspose.Imaging.FileFormats.Cdr.CdrImage image = (Aspose.Imaging.FileFormats.Cdr.CdrImage)Aspose.Imaging.Image.Load(path))
  677. {
  678. PngOptions options = new Aspose.Imaging.ImageOptions.PngOptions();
  679. options.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
  680. // Set rasterization options for fileformat
  681. options.VectorRasterizationOptions = (Aspose.Imaging.ImageOptions.VectorRasterizationOptions)
  682. image.GetDefaultOptions(new object[] { Aspose.Imaging.Color.White, image.Width, image.Height });
  683. options.VectorRasterizationOptions.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  684. options.VectorRasterizationOptions.SmoothingMode = Aspose.Imaging.SmoothingMode.None;
  685. targPath = targPath.Replace(".cdr", ".png");
  686. image.Save(targPath, options);
  687. options.Dispose();
  688. }
  689. }
  690. private string CdrExPng(string path, string targPath)
  691. {
  692. try
  693. {
  694. targPath = targPath.Replace(".cdr", ".png");
  695. Application cdr = new Application();
  696. cdr.OpenDocument(path, 1);
  697. cdr.ActiveDocument.ExportBitmap(
  698. targPath,
  699. cdrFilter.cdrPNG,
  700. cdrExportRange.cdrCurrentPage,
  701. cdrImageType.cdrRGBColorImage,
  702. 0, 0, 72, 72,
  703. cdrAntiAliasingType.cdrNoAntiAliasing,
  704. false,
  705. true,
  706. true,
  707. false,
  708. cdrCompressionType.cdrCompressionNone,
  709. null).Finish();
  710. cdr.ActiveDocument.Close();
  711. cdr.Quit();
  712. cdr = null;
  713. GC.Collect();
  714. }
  715. catch (Exception ex)
  716. {
  717. XLog.SaveLog(0, path + ",转成图片出错:" + ex.Message);
  718. }
  719. finally
  720. {
  721. }
  722. return "111";
  723. }
  724. private static object cdrpngobj = new object();
  725. private void CdrExportPng(string path, string cdrFile)
  726. {
  727. lock (cdrpngobj)
  728. {
  729. string fname = curPath + "\\" + Path.GetFileNameWithoutExtension(cdrFile) + ".png";
  730. string new_fname = path + "\\" + Path.GetFileNameWithoutExtension(cdrFile) + ".png";
  731. CorelDRAW.Application cdr = new CorelDRAW.Application();
  732. cdr.OpenDocument(cdrFile, 1);
  733. cdr.ActiveDocument.ExportBitmap(
  734. fname,
  735. CorelDRAW.cdrFilter.cdrPNG,
  736. CorelDRAW.cdrExportRange.cdrCurrentPage,
  737. CorelDRAW.cdrImageType.cdrRGBColorImage,
  738. 0, 0, 72, 72,
  739. CorelDRAW.cdrAntiAliasingType.cdrNoAntiAliasing,
  740. false,
  741. true,
  742. true,
  743. false,
  744. CorelDRAW.cdrCompressionType.cdrCompressionNone,
  745. null).Finish();
  746. cdr.ActiveDocument.Close();
  747. cdr.Quit();
  748. if (File.Exists(fname))
  749. {
  750. File.Copy(fname, new_fname);
  751. File.Delete(fname);
  752. }
  753. }
  754. }
  755. public static string MidStrEx(string sourse, string startstr, string endstr)
  756. {
  757. string result = string.Empty;
  758. int startindex, endindex;
  759. try
  760. {
  761. startindex = sourse.IndexOf(startstr);
  762. if (startindex == -1)
  763. return result;
  764. string tmpstr = sourse.Substring(startindex + startstr.Length);
  765. endindex = tmpstr.IndexOf(endstr);
  766. if (endindex == -1)
  767. return result;
  768. result = tmpstr.Remove(endindex);
  769. }
  770. catch (Exception ex)
  771. {
  772. Console.WriteLine("MidStrEx Err:" + ex.Message);
  773. }
  774. return result;
  775. }
  776. private static string logPath = ConfigurationManager.AppSettings["curPath"] + "\\log";
  777. private static object logFlag = new object();
  778. public static void writeLog(string log)
  779. {
  780. lock (logFlag)
  781. {
  782. using (FileStream fileStream = new FileStream(logPath + "\\" + DateTime.Now.ToString("yy-MM-dd") + ".log", FileMode.Append, FileAccess.Write))
  783. {
  784. using (StreamWriter sw = new StreamWriter(fileStream, Encoding.Default))
  785. {
  786. sw.Write(log + " ------时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
  787. sw.Flush();
  788. }
  789. }
  790. }
  791. }
  792. }