ctrlFileDown.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Collections;
  11. using Core.Mes.Client.Comm.Server;
  12. using System.Diagnostics;
  13. using Core.Mes.Client.Comm.Control;
  14. using Core.Mes.Client.Comm.Tool;
  15. namespace Core.Mes.Client.Comm.Control
  16. {
  17. public delegate void FileChange(string fileName);
  18. /// <summary>
  19. /// 关闭窗体并传出文件夹
  20. /// </summary>
  21. /// <param name="path"></param>
  22. public delegate void FileClose(string path);
  23. public partial class ctrlFileDown : UserControl
  24. {
  25. //坐标系数
  26. private double _btn1 = 0;
  27. private double _btn2 = 0;
  28. private double _btn3 = 0;
  29. private double _lbl2X = 0;
  30. private double _lbl2Y = 0;
  31. /// <summary>
  32. /// 是否批量下载 默认否
  33. /// </summary>
  34. private string batchFlag = "0";
  35. public string BatchFlag
  36. {
  37. get { return batchFlag; }
  38. set { batchFlag = value; }
  39. }
  40. private List<string> folderList = new List<string>();
  41. public List<string> FolderList
  42. {
  43. get { return folderList; }
  44. set { folderList = value; }
  45. }
  46. public void SetPathList(List<string> list)
  47. {
  48. FlileDownBatch(list);
  49. }
  50. public void SetPathList(List<string> list, string batchFlag, List<string> FolderList)
  51. {
  52. FlileDownBatch(list, batchFlag, FolderList);
  53. }
  54. public ctrlFileDown()
  55. {
  56. InitializeComponent();
  57. if (this.DesignMode) return;
  58. _btn1 = (double)button1.Location.X / (double)this.panel1.Width;
  59. _btn2 = (double)button2.Location.X / (double)this.panel1.Width;
  60. _btn3 = (double)button3.Location.X / (double)this.panel1.Width;
  61. _lbl2X = (double)label2.Location.X / (double)this.pictureBox1.Width;
  62. _lbl2Y = (double)label2.Location.Y / (double)this.pictureBox1.Height;
  63. }
  64. private string strPath = "";//定义文件路径字段
  65. private List<Image> ing = new List<Image>();
  66. private ArrayList ingname = new ArrayList();
  67. private List<FileBean> list = new List<FileBean>();
  68. private string currentFileName = "";
  69. public event FileChange FileChangeEvent;
  70. public event FileClose FileCloseEvent;
  71. public List<FileBean> List
  72. {
  73. get { return list; }
  74. set { list = value; }
  75. }
  76. int i = 0;
  77. int j = 0;
  78. int a = 0;
  79. bool fal = false;
  80. public string spathName = "";
  81. public string FilePath
  82. {
  83. get
  84. {
  85. return spathName;
  86. }
  87. set
  88. {
  89. spathName = value;
  90. FlileDown(spathName);
  91. }
  92. }
  93. public void FlileDown(string pathName)
  94. {
  95. if (this.DesignMode) return;
  96. if (pathName == "" || pathName == null) return;
  97. List<FileBean> list = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
  98. ShowByListFileBean(list);
  99. }
  100. /// <summary>
  101. /// 批量下载文件
  102. /// </summary>
  103. /// <param name="pathList"></param>
  104. public void FlileDownBatch(List<string> pathList)
  105. {
  106. if (this.DesignMode) return;
  107. if (pathList == null || pathList.Count <= 0) return;
  108. List<FileBean> list = new List<FileBean>();
  109. for (int i = 0; i < pathList.Count; i++)
  110. {
  111. string pathName = pathList[i];
  112. List<FileBean> tepList = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
  113. if (tepList != null && tepList.Count > 0)
  114. {
  115. list.Add(tepList[0]);
  116. }
  117. }
  118. ShowByListFileBean(list);
  119. }
  120. public void FlileDownBatch(List<string> pathList, string batchFlag, List<string> FolderList)
  121. {
  122. if (this.DesignMode) return;
  123. if (pathList == null || pathList.Count <= 0) return;
  124. List<FileBean> list = new List<FileBean>();
  125. for (int i = 0; i < pathList.Count; i++)
  126. {
  127. string[] a = pathList[i].Split(';');
  128. string pathName = a[1];
  129. List<FileBean> tepList = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
  130. if (tepList != null && tepList.Count > 0)
  131. {
  132. if (batchFlag == "2")
  133. {
  134. for (int j = 0; j < tepList.Count;j++ )
  135. {
  136. tepList[j].setPathName("/");
  137. list.Add(tepList[j]);
  138. FolderList.Add(a[0]);
  139. }
  140. }
  141. else {
  142. list.Add(tepList[0]);
  143. FolderList.Add(a[0]);
  144. }
  145. }
  146. }
  147. ShowByListFileBean(list);
  148. }
  149. /// <summary>
  150. /// 通过文件集合(图片)显示出来。
  151. /// </summary>
  152. /// <param name="list"></param>
  153. public void ShowByListFileBean(List<FileBean> list)
  154. {
  155. if (list == null) return;
  156. j = 0;
  157. i = 0;
  158. ing.Clear();
  159. ingname.Clear();
  160. this.list = list;
  161. ClearTmp();
  162. foreach (FileBean bean in list)
  163. {
  164. Image image;
  165. try
  166. {
  167. image = FileHelper.BytesToBitmap(bean.getFile());
  168. }
  169. catch
  170. {
  171. image = null;
  172. WriteBytesToTmp(bean.getFile(), bean.getFileName());
  173. }
  174. ing.Add(image);
  175. ingname.Add(bean.getFileName());
  176. if (ing[0] != null)
  177. {
  178. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[0], pictureBox1);
  179. }
  180. this.currentFileName = list[0].getFileName();
  181. if (FileChangeEvent != null)
  182. {
  183. FileChangeEvent(currentFileName);
  184. }
  185. j = j + 1;
  186. }
  187. if (ing.Count == 0)
  188. {
  189. button1.Visible = false;
  190. button2.Visible = false;
  191. fal = false;
  192. this.pictureBox1.Image = null;
  193. this.currentFileName = "";
  194. if (FileChangeEvent != null)
  195. {
  196. FileChangeEvent(currentFileName);
  197. }
  198. // MessageBox.Show("文件夹是空的", "提示"); LX 修改
  199. label1.Text = "0/0";
  200. }
  201. else
  202. {
  203. button1.Visible = true;
  204. button2.Visible = true;
  205. fal = true;
  206. label1.Text = "1/" + j;
  207. }
  208. ShowDontViewBnt();
  209. }
  210. private void WriteBytesToTmp(byte[] Bytes, string fileName)
  211. {
  212. try
  213. {
  214. string tmpPath = Environment.CurrentDirectory + "\\Tmp\\";
  215. FileStream fs = new FileStream(tmpPath + fileName, FileMode.Create);
  216. fs.Write(Bytes, 0, Bytes.Length);
  217. fs.Flush();
  218. fs.Close();
  219. }
  220. catch { }
  221. }
  222. private void ClearTmp()
  223. {
  224. string tmpPath = Environment.CurrentDirectory + "\\Tmp\\";
  225. DirectoryInfo di = new DirectoryInfo(tmpPath);
  226. if (di.Exists == fal)
  227. {
  228. di.Create();
  229. return;
  230. }
  231. foreach (FileInfo fi in di.GetFiles())
  232. {
  233. try
  234. {
  235. fi.Delete();
  236. }
  237. catch { continue; }
  238. }
  239. }
  240. private void button2_Click(object sender, EventArgs e)//上一张
  241. {
  242. if (fal)
  243. {
  244. this.pictureBox1.Image = null;
  245. if (i == 0)
  246. {
  247. i = j - 1;
  248. if (ing[i] != null)
  249. {
  250. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
  251. }
  252. this.currentFileName = list[i].getFileName();
  253. label1.Text = i + 1 + "/" + j;
  254. }
  255. else
  256. {
  257. i = i - 1;
  258. if (ing[i] != null)
  259. {
  260. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
  261. }
  262. this.currentFileName = list[i].getFileName();
  263. label1.Text = i + 1 + "/" + j;
  264. }
  265. if (FileChangeEvent != null)
  266. {
  267. FileChangeEvent(currentFileName);
  268. }
  269. ShowDontViewBnt();
  270. }
  271. }
  272. private void button1_Click(object sender, EventArgs e)//下一张
  273. {
  274. if (fal)
  275. {
  276. this.pictureBox1.Image = null;
  277. if (i + 1 == j)
  278. {
  279. i = 0;
  280. if (ing[i] != null)
  281. {
  282. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
  283. }
  284. this.currentFileName = list[i].getFileName();
  285. label1.Text = i + 1 + "/" + j;
  286. }
  287. else
  288. {
  289. i = i + 1;
  290. if (ing[i] != null)
  291. {
  292. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
  293. }
  294. this.currentFileName = list[i].getFileName();
  295. label1.Text = i + 1 + "/" + j;
  296. }
  297. if (FileChangeEvent != null)
  298. {
  299. FileChangeEvent(currentFileName);
  300. }
  301. ShowDontViewBnt();
  302. }
  303. }
  304. private void button3_Click(object sender, EventArgs e)
  305. {
  306. if (ingname.Count == 0)
  307. {
  308. MessageUtil.ShowWarning("没有可删除的文件!");
  309. return;
  310. }
  311. if (MessageBox.Show("是否确定要进行删除操作", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) //LX 修改
  312. {
  313. bool isSuccess = Core.Mes.Client.Comm.Server.FileHelper.Delete(spathName + "/" + ingname[i] + ""); ;
  314. if (isSuccess)
  315. {
  316. MessageUtil.ShowTips("删除成功!");
  317. FlileDown(spathName);
  318. }
  319. }
  320. }
  321. private void ShowDontViewBnt()
  322. {
  323. if (this.pictureBox1.Image == null && ing.Count > 0)
  324. {
  325. label2.Visible = true;
  326. button4.Visible = true;
  327. label2.Text = "无法浏览文件:" + ingname[i];
  328. }
  329. else
  330. {
  331. label2.Visible = false;
  332. button4.Visible = false;
  333. }
  334. }
  335. private void pictureBox1_SizeChanged(object sender, EventArgs e)
  336. {
  337. if (this.DesignMode == true) return;
  338. if (_lbl2X == 0) return;
  339. label2.Location = new Point((int)((double)pictureBox1.Width * _lbl2X),
  340. (int)((double)pictureBox1.Height * _lbl2Y));
  341. button4.Location = new Point(label2.Location.X + 45, label2.Location.Y + 35);
  342. }
  343. private void panel1_SizeChanged(object sender, EventArgs e)
  344. {
  345. if (this.DesignMode == true) return;
  346. if (_btn1 == 0) return;
  347. button1.Location = new Point((int)((double)panel1.Width * _btn1), button1.Location.Y);
  348. button2.Location = new Point((int)((double)panel1.Width * _btn2), button1.Location.Y);
  349. button3.Location = new Point((int)((double)panel1.Width * _btn3), button1.Location.Y);
  350. button5.Location = new Point(button3.Location.X - 103, button3.Location.Y);
  351. }
  352. private void button4_Click(object sender, EventArgs e)
  353. {
  354. string filePath = Environment.CurrentDirectory + "\\Tmp\\" + ingname[i];
  355. Process.Start(filePath);
  356. }
  357. private void button5_Click(object sender, EventArgs e)
  358. {
  359. if (ing.Count == 0)
  360. {
  361. MessageUtil.ShowWarning("没有可下载的文件!");
  362. return;
  363. }
  364. if (batchFlag == "0")
  365. {
  366. SaveFileDialog file = new SaveFileDialog();
  367. string fileType = list[i].getFileName().Split('.')[list[i].getFileName().Split('.').Length - 1];
  368. file.Filter = "Document(*." + fileType + ")| *." + fileType;
  369. file.FileName = list[i].getFileName();
  370. DialogResult drStat = file.ShowDialog();
  371. if (drStat == DialogResult.OK)
  372. {
  373. FileStream fs = new FileStream(file.FileName, FileMode.Create);
  374. byte[] Bytes = list[i].getFile();
  375. fs.Write(Bytes, 0, Bytes.Length);
  376. fs.Flush();
  377. fs.Close();
  378. }
  379. }
  380. else if (batchFlag == "1")
  381. {
  382. FolderBrowserDialog fbd = new FolderBrowserDialog();
  383. fbd.ShowNewFolderButton = true;
  384. DialogResult drStat = fbd.ShowDialog();
  385. if (drStat == DialogResult.OK)
  386. {
  387. string path = fbd.SelectedPath;
  388. for (int index = 0; index < list.Count; index++)
  389. {
  390. string fileName = list[index].getFileName();
  391. string pathNew = path + "\\" + fileName;
  392. FileStream fs = new FileStream(pathNew, FileMode.Create);
  393. byte[] Bytes = list[index].getFile();
  394. fs.Write(Bytes, 0, Bytes.Length);
  395. fs.Flush();
  396. fs.Close();
  397. }
  398. if (FileCloseEvent != null)
  399. FileCloseEvent(path);
  400. }
  401. }
  402. else if (batchFlag == "2")
  403. {
  404. FolderBrowserDialog fbd = new FolderBrowserDialog();
  405. fbd.ShowNewFolderButton = true;
  406. DialogResult drStat = fbd.ShowDialog();
  407. if (drStat == DialogResult.OK)
  408. {
  409. string path = fbd.SelectedPath;
  410. for (int index = 0; index < list.Count; index++)
  411. {
  412. string folderName = folderList[index];
  413. string fileName = list[index].getFileName();
  414. if (!Directory.Exists(path + "\\" + folderName + "\\" + list[index].getPathName()))
  415. {
  416. Directory.CreateDirectory(path + "\\" + folderName + "\\" + list[index].getPathName());
  417. }
  418. string pathNew = path + "\\" + folderName + "\\" + list[index].getPathName() + "\\" + fileName;
  419. FileStream fs = new FileStream(pathNew, FileMode.Create);
  420. byte[] Bytes = list[index].getFile();
  421. fs.Write(Bytes, 0, Bytes.Length);
  422. fs.Flush();
  423. fs.Close();
  424. }
  425. if (FileCloseEvent != null)
  426. FileCloseEvent(path);
  427. }
  428. }
  429. }
  430. }
  431. }