ctrlFileDown.cs 11 KB

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