ctrlFileDown.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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.StlMes.Client.GlBusiness
  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. public void SetPathList(List<string> list)
  41. {
  42. FlileDownBatch(list);
  43. }
  44. public ctrlFileDown()
  45. {
  46. InitializeComponent();
  47. if (this.DesignMode) return;
  48. _btn1 = (double)button1.Location.X / (double)this.panel1.Width;
  49. _btn2 = (double)button2.Location.X / (double)this.panel1.Width;
  50. _btn3 = (double)button3.Location.X / (double)this.panel1.Width;
  51. _lbl2X = (double)label2.Location.X / (double)this.pictureBox1.Width;
  52. _lbl2Y = (double)label2.Location.Y / (double)this.pictureBox1.Height;
  53. }
  54. private string strPath = "";//定义文件路径字段
  55. private List<Image> ing = new List<Image>();
  56. private ArrayList ingname = new ArrayList();
  57. private List<FileBean> list = new List<FileBean>();
  58. private string currentFileName = "";
  59. public event FileChange FileChangeEvent;
  60. public event FileClose FileCloseEvent;
  61. public List<FileBean> List
  62. {
  63. get { return list; }
  64. set { list = value; }
  65. }
  66. int i = 0;
  67. int j = 0;
  68. int a = 0;
  69. bool fal = false;
  70. public string spathName = "";
  71. public string FilePath
  72. {
  73. get
  74. {
  75. return spathName;
  76. }
  77. set
  78. {
  79. spathName = value;
  80. FlileDown(spathName);
  81. }
  82. }
  83. public void FlileDown(string pathName)
  84. {
  85. if (this.DesignMode) return;
  86. if (pathName == "" || pathName == null) return;
  87. List<FileBean> list = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
  88. ShowByListFileBean(list);
  89. }
  90. /// <summary>
  91. /// 批量下载文件
  92. /// </summary>
  93. /// <param name="pathList"></param>
  94. public void FlileDownBatch(List<string> pathList)
  95. {
  96. if (this.DesignMode) return;
  97. if (pathList == null || pathList.Count <= 0) return;
  98. List<FileBean> list = new List<FileBean>();
  99. for (int i = 0; i < pathList.Count; i++)
  100. {
  101. string pathName = pathList[i];
  102. List<FileBean> tepList = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
  103. if (tepList != null && tepList.Count > 0)
  104. {
  105. list.Add(tepList[0]);
  106. }
  107. }
  108. ShowByListFileBean(list);
  109. }
  110. /// <summary>
  111. /// 通过文件集合(图片)显示出来。
  112. /// </summary>
  113. /// <param name="list"></param>
  114. public void ShowByListFileBean(List<FileBean> list)
  115. {
  116. if (list == null) return;
  117. j = 0;
  118. i = 0;
  119. ing.Clear();
  120. ingname.Clear();
  121. this.list = list;
  122. ClearTmp();
  123. foreach (FileBean bean in list)
  124. {
  125. Image image;
  126. try
  127. {
  128. image = FileHelper.BytesToBitmap(bean.getFile());
  129. }
  130. catch
  131. {
  132. image = null;
  133. WriteBytesToTmp(bean.getFile(), bean.getFileName());
  134. }
  135. ing.Add(image);
  136. ingname.Add(bean.getFileName());
  137. if (ing[0] != null)
  138. {
  139. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[0], pictureBox1);
  140. }
  141. this.currentFileName = list[0].getFileName();
  142. if (FileChangeEvent != null)
  143. {
  144. FileChangeEvent(currentFileName);
  145. }
  146. j = j + 1;
  147. }
  148. if (ing.Count == 0)
  149. {
  150. button1.Visible = false;
  151. button2.Visible = false;
  152. fal = false;
  153. this.pictureBox1.Image = null;
  154. this.currentFileName = "";
  155. if (FileChangeEvent != null)
  156. {
  157. FileChangeEvent(currentFileName);
  158. }
  159. // MessageBox.Show("文件夹是空的", "提示"); LX 修改
  160. label1.Text = "0/0";
  161. }
  162. else
  163. {
  164. button1.Visible = true;
  165. button2.Visible = true;
  166. fal = true;
  167. label1.Text = "1/" + j;
  168. }
  169. ShowDontViewBnt();
  170. }
  171. private void WriteBytesToTmp(byte[] Bytes, string fileName)
  172. {
  173. try
  174. {
  175. string tmpPath = Environment.CurrentDirectory + "\\Tmp\\";
  176. FileStream fs = new FileStream(tmpPath + fileName, FileMode.Create);
  177. fs.Write(Bytes, 0, Bytes.Length);
  178. fs.Flush();
  179. fs.Close();
  180. }
  181. catch { }
  182. }
  183. private void ClearTmp()
  184. {
  185. string tmpPath = Environment.CurrentDirectory + "\\Tmp\\";
  186. DirectoryInfo di = new DirectoryInfo(tmpPath);
  187. if (di.Exists == fal)
  188. {
  189. di.Create();
  190. return;
  191. }
  192. foreach (FileInfo fi in di.GetFiles())
  193. {
  194. try
  195. {
  196. fi.Delete();
  197. }
  198. catch { continue; }
  199. }
  200. }
  201. private void button2_Click(object sender, EventArgs e)//上一张
  202. {
  203. if (fal)
  204. {
  205. this.pictureBox1.Image = null;
  206. if (i == 0)
  207. {
  208. i = j - 1;
  209. if (ing[i] != null)
  210. {
  211. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
  212. }
  213. this.currentFileName = list[i].getFileName();
  214. label1.Text = i + 1 + "/" + j;
  215. }
  216. else
  217. {
  218. i = i - 1;
  219. if (ing[i] != null)
  220. {
  221. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
  222. }
  223. this.currentFileName = list[i].getFileName();
  224. label1.Text = i + 1 + "/" + j;
  225. }
  226. if (FileChangeEvent != null)
  227. {
  228. FileChangeEvent(currentFileName);
  229. }
  230. ShowDontViewBnt();
  231. }
  232. }
  233. private void button1_Click(object sender, EventArgs e)//下一张
  234. {
  235. if (fal)
  236. {
  237. this.pictureBox1.Image = null;
  238. if (i + 1 == j)
  239. {
  240. i = 0;
  241. if (ing[i] != null)
  242. {
  243. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
  244. }
  245. this.currentFileName = list[i].getFileName();
  246. label1.Text = i + 1 + "/" + j;
  247. }
  248. else
  249. {
  250. i = i + 1;
  251. if (ing[i] != null)
  252. {
  253. PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
  254. }
  255. this.currentFileName = list[i].getFileName();
  256. label1.Text = i + 1 + "/" + j;
  257. }
  258. if (FileChangeEvent != null)
  259. {
  260. FileChangeEvent(currentFileName);
  261. }
  262. ShowDontViewBnt();
  263. }
  264. }
  265. private void button3_Click(object sender, EventArgs e)
  266. {
  267. if (ingname.Count == 0)
  268. {
  269. MessageUtil.ShowWarning("没有可删除的文件!");
  270. return;
  271. }
  272. if (MessageBox.Show("是否确定要进行删除操作", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) //LX 修改
  273. {
  274. bool isSuccess = Core.Mes.Client.Comm.Server.FileHelper.Delete(spathName + "/" + ingname[i] + ""); ;
  275. if (isSuccess)
  276. {
  277. MessageUtil.ShowTips("删除成功!");
  278. FlileDown(spathName);
  279. }
  280. }
  281. }
  282. private void ShowDontViewBnt()
  283. {
  284. if (this.pictureBox1.Image == null && ing.Count > 0)
  285. {
  286. label2.Visible = true;
  287. button4.Visible = true;
  288. label2.Text = "无法浏览文件:" + ingname[i];
  289. }
  290. else
  291. {
  292. label2.Visible = false;
  293. button4.Visible = false;
  294. }
  295. }
  296. private void pictureBox1_SizeChanged(object sender, EventArgs e)
  297. {
  298. if (this.DesignMode == true) return;
  299. if (_lbl2X == 0) return;
  300. label2.Location = new Point((int)((double)pictureBox1.Width * _lbl2X),
  301. (int)((double)pictureBox1.Height * _lbl2Y));
  302. button4.Location = new Point(label2.Location.X + 45, label2.Location.Y + 35);
  303. }
  304. private void panel1_SizeChanged(object sender, EventArgs e)
  305. {
  306. if (this.DesignMode == true) return;
  307. if (_btn1 == 0) return;
  308. button1.Location = new Point((int)((double)panel1.Width * _btn1), button1.Location.Y);
  309. button2.Location = new Point((int)((double)panel1.Width * _btn2), button1.Location.Y);
  310. button3.Location = new Point((int)((double)panel1.Width * _btn3), button1.Location.Y);
  311. button5.Location = new Point(button3.Location.X - 103, button3.Location.Y);
  312. }
  313. private void button4_Click(object sender, EventArgs e)
  314. {
  315. string filePath = Environment.CurrentDirectory + "\\Tmp\\" + ingname[i];
  316. Process.Start(filePath);
  317. }
  318. private void button5_Click(object sender, EventArgs e)
  319. {
  320. if (ing.Count == 0)
  321. {
  322. MessageUtil.ShowWarning("没有可下载的文件!");
  323. return;
  324. }
  325. if (batchFlag == "0")
  326. {
  327. SaveFileDialog file = new SaveFileDialog();
  328. string fileType = list[i].getFileName().Split('.')[list[i].getFileName().Split('.').Length - 1];
  329. file.Filter = "Document(*." + fileType + ")| *." + fileType;
  330. file.FileName = list[i].getFileName();
  331. DialogResult drStat = file.ShowDialog();
  332. if (drStat == DialogResult.OK)
  333. {
  334. FileStream fs = new FileStream(file.FileName, FileMode.Create);
  335. byte[] Bytes = list[i].getFile();
  336. fs.Write(Bytes, 0, Bytes.Length);
  337. fs.Flush();
  338. fs.Close();
  339. }
  340. }
  341. else if (batchFlag == "1")
  342. {
  343. FolderBrowserDialog fbd = new FolderBrowserDialog();
  344. fbd.ShowNewFolderButton = true;
  345. DialogResult drStat = fbd.ShowDialog();
  346. if (drStat == DialogResult.OK)
  347. {
  348. string path = fbd.SelectedPath;
  349. for (int index = 0; index < list.Count; index++)
  350. {
  351. string fileName = list[index].getFileName();
  352. string pathNew = path + "\\" + fileName;
  353. FileStream fs = new FileStream(pathNew, FileMode.Create);
  354. byte[] Bytes = list[index].getFile();
  355. fs.Write(Bytes, 0, Bytes.Length);
  356. fs.Flush();
  357. fs.Close();
  358. }
  359. if (FileCloseEvent != null)
  360. FileCloseEvent(path);
  361. }
  362. }
  363. }
  364. }
  365. }