ctrlFileDown.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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.YdmStuffManage
  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. this.pictureBox1.Image = ing[0];
  138. this.currentFileName = list[0].getFileName();
  139. if (FileChangeEvent != null)
  140. {
  141. FileChangeEvent(currentFileName);
  142. }
  143. PictureBoxHelper.LoadThumbnailImage(pictureBox1);
  144. j = j + 1;
  145. }
  146. if (ing.Count == 0)
  147. {
  148. button1.Visible = false;
  149. button2.Visible = false;
  150. fal = false;
  151. this.pictureBox1.Image = null;
  152. this.currentFileName = "";
  153. if (FileChangeEvent != null)
  154. {
  155. FileChangeEvent(currentFileName);
  156. }
  157. // MessageBox.Show("文件夹是空的", "提示"); LX 修改
  158. label1.Text = "0/0";
  159. }
  160. else
  161. {
  162. button1.Visible = true;
  163. button2.Visible = true;
  164. fal = true;
  165. label1.Text = "1/" + j;
  166. }
  167. ShowDontViewBnt();
  168. }
  169. private void WriteBytesToTmp(byte[] Bytes, string fileName)
  170. {
  171. try
  172. {
  173. string tmpPath = Environment.CurrentDirectory + "\\Tmp\\";
  174. FileStream fs = new FileStream(tmpPath + fileName, FileMode.Create);
  175. fs.Write(Bytes, 0, Bytes.Length);
  176. fs.Flush();
  177. fs.Close();
  178. }
  179. catch { }
  180. }
  181. private void ClearTmp()
  182. {
  183. string tmpPath = Environment.CurrentDirectory + "\\Tmp\\";
  184. DirectoryInfo di = new DirectoryInfo(tmpPath);
  185. if (di.Exists == fal)
  186. {
  187. di.Create();
  188. return;
  189. }
  190. foreach (FileInfo fi in di.GetFiles())
  191. {
  192. try
  193. {
  194. fi.Delete();
  195. }
  196. catch { continue; }
  197. }
  198. }
  199. private void button2_Click(object sender, EventArgs e)//上一张
  200. {
  201. if (fal)
  202. {
  203. if (i == 0)
  204. {
  205. i = j - 1;
  206. this.pictureBox1.Image = ing[i];//显示图像
  207. this.currentFileName = list[i].getFileName();
  208. PictureBoxHelper.LoadThumbnailImage(pictureBox1);
  209. label1.Text = i + 1 + "/" + j;
  210. }
  211. else
  212. {
  213. i = i - 1;
  214. this.pictureBox1.Image = ing[i];//显示图像
  215. this.currentFileName = list[i].getFileName();
  216. PictureBoxHelper.LoadThumbnailImage(pictureBox1);
  217. label1.Text = i + 1 + "/" + j;
  218. }
  219. if (FileChangeEvent != null)
  220. {
  221. FileChangeEvent(currentFileName);
  222. }
  223. ShowDontViewBnt();
  224. }
  225. }
  226. private void button1_Click(object sender, EventArgs e)//下一张
  227. {
  228. if (fal)
  229. {
  230. if (i + 1 == j)
  231. {
  232. i = 0;
  233. this.pictureBox1.Image = ing[i];//显示图像
  234. this.currentFileName = list[i].getFileName();
  235. PictureBoxHelper.LoadThumbnailImage(pictureBox1);
  236. label1.Text = i + 1 + "/" + j;
  237. }
  238. else
  239. {
  240. i = i + 1;
  241. this.pictureBox1.Image = ing[i];//显示图像
  242. this.currentFileName = list[i].getFileName();
  243. PictureBoxHelper.LoadThumbnailImage(pictureBox1);
  244. label1.Text = i + 1 + "/" + j;
  245. }
  246. if (FileChangeEvent != null)
  247. {
  248. FileChangeEvent(currentFileName);
  249. }
  250. ShowDontViewBnt();
  251. }
  252. }
  253. private void button3_Click(object sender, EventArgs e)
  254. {
  255. if (ingname.Count == 0)
  256. {
  257. MessageUtil.ShowWarning("没有可删除的文件!");
  258. return;
  259. }
  260. if (MessageBox.Show("是否确定要进行删除操作", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) //LX 修改
  261. {
  262. bool isSuccess = Core.Mes.Client.Comm.Server.FileHelper.Delete(spathName + "/" + ingname[i] + ""); ;
  263. if (isSuccess)
  264. {
  265. MessageUtil.ShowTips("删除成功!");
  266. FlileDown(spathName);
  267. }
  268. }
  269. }
  270. private void ShowDontViewBnt()
  271. {
  272. if (this.pictureBox1.Image == null && ing.Count > 0)
  273. {
  274. label2.Visible = true;
  275. button4.Visible = true;
  276. label2.Text = "无法浏览文件:" + ingname[i];
  277. }
  278. else
  279. {
  280. label2.Visible = false;
  281. button4.Visible = false;
  282. }
  283. }
  284. private void pictureBox1_SizeChanged(object sender, EventArgs e)
  285. {
  286. if (this.DesignMode == true) return;
  287. if (_lbl2X == 0) return;
  288. label2.Location = new Point((int)((double)pictureBox1.Width * _lbl2X),
  289. (int)((double)pictureBox1.Height * _lbl2Y));
  290. button4.Location = new Point(label2.Location.X + 45, label2.Location.Y + 35);
  291. }
  292. private void panel1_SizeChanged(object sender, EventArgs e)
  293. {
  294. if (this.DesignMode == true) return;
  295. if (_btn1 == 0) return;
  296. button1.Location = new Point((int)((double)panel1.Width * _btn1), button1.Location.Y);
  297. button2.Location = new Point((int)((double)panel1.Width * _btn2), button1.Location.Y);
  298. button3.Location = new Point((int)((double)panel1.Width * _btn3), button1.Location.Y);
  299. button5.Location = new Point(button3.Location.X - 103, button3.Location.Y);
  300. }
  301. private void button4_Click(object sender, EventArgs e)
  302. {
  303. string filePath = Environment.CurrentDirectory + "\\Tmp\\" + ingname[i];
  304. Process.Start(filePath);
  305. }
  306. private void button5_Click(object sender, EventArgs e)
  307. {
  308. if (ing.Count == 0)
  309. {
  310. MessageUtil.ShowWarning("没有可下载的文件!");
  311. return;
  312. }
  313. if (batchFlag == "0")
  314. {
  315. SaveFileDialog file = new SaveFileDialog();
  316. string fileType = list[i].getFileName().Split('.')[list[i].getFileName().Split('.').Length - 1];
  317. file.Filter = "Document(*." + fileType + ")| *." + fileType;
  318. file.FileName = list[i].getFileName();
  319. DialogResult drStat = file.ShowDialog();
  320. if (drStat == DialogResult.OK)
  321. {
  322. FileStream fs = new FileStream(file.FileName, FileMode.Create);
  323. byte[] Bytes = list[i].getFile();
  324. fs.Write(Bytes, 0, Bytes.Length);
  325. fs.Flush();
  326. fs.Close();
  327. }
  328. }
  329. else if (batchFlag == "1")
  330. {
  331. FolderBrowserDialog fbd = new FolderBrowserDialog();
  332. fbd.ShowNewFolderButton = true;
  333. DialogResult drStat = fbd.ShowDialog();
  334. if (drStat == DialogResult.OK)
  335. {
  336. string path = fbd.SelectedPath;
  337. for (int index = 0; index < list.Count; index++)
  338. {
  339. string fileName = list[index].getFileName();
  340. string pathNew = path + "\\" + fileName;
  341. FileStream fs = new FileStream(pathNew, FileMode.Create);
  342. byte[] Bytes = list[index].getFile();
  343. fs.Write(Bytes, 0, Bytes.Length);
  344. fs.Flush();
  345. fs.Close();
  346. }
  347. if (FileCloseEvent != null)
  348. FileCloseEvent(path);
  349. }
  350. }
  351. }
  352. }
  353. }