| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Collections;
- using Core.Mes.Client.Comm.Server;
- using System.Diagnostics;
- using Core.Mes.Client.Comm.Control;
- using Core.Mes.Client.Comm.Tool;
- namespace Core.StlMes.Client.ZGMil.ResultConrtrol
- {
- public delegate void FileChange(string fileName);
- /// <summary>
- /// 关闭窗体并传出文件夹
- /// </summary>
- /// <param name="path"></param>
- public delegate void FileClose(string path);
- public partial class ctrlFileDown : UserControl
- {
- //坐标系数
- private double _btn1 = 0;
- private double _btn2 = 0;
- private double _btn3 = 0;
- private double _lbl2X = 0;
- private double _lbl2Y = 0;
- /// <summary>
- /// 是否批量下载 默认否
- /// </summary>
- private string batchFlag = "0";
- public string BatchFlag
- {
- get { return batchFlag; }
- set { batchFlag = value; }
- }
- public void SetPathList(List<string> list)
- {
- FlileDownBatch(list);
- }
- public ctrlFileDown()
- {
- InitializeComponent();
- if (this.DesignMode) return;
- _btn1 = (double)button1.Location.X / (double)this.panel1.Width;
- _btn2 = (double)button2.Location.X / (double)this.panel1.Width;
- _btn3 = (double)button3.Location.X / (double)this.panel1.Width;
- _lbl2X = (double)label2.Location.X / (double)this.pictureBox1.Width;
- _lbl2Y = (double)label2.Location.Y / (double)this.pictureBox1.Height;
- }
- private string strPath = "";//定义文件路径字段
- private List<Image> ing = new List<Image>();
- private ArrayList ingname = new ArrayList();
- private List<FileBean> list = new List<FileBean>();
- private string currentFileName = "";
- public event FileChange FileChangeEvent;
- public event FileClose FileCloseEvent;
- public List<FileBean> List
- {
- get { return list; }
- set { list = value; }
- }
- int i = 0;
- int j = 0;
- int a = 0;
- bool fal = false;
- public string spathName = "";
- public string FilePath
- {
- get
- {
- return spathName;
- }
- set
- {
- spathName = value;
- FlileDown(spathName);
- }
- }
- public void FlileDown(string pathName)
- {
- if (this.DesignMode) return;
- if (pathName == "" || pathName == null) return;
- List<FileBean> list = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
- ShowByListFileBean(list);
- }
- /// <summary>
- /// 批量下载文件
- /// </summary>
- /// <param name="pathList"></param>
- public void FlileDownBatch(List<string> pathList)
- {
- if (this.DesignMode) return;
- if (pathList == null || pathList.Count <= 0) return;
- List<FileBean> list = new List<FileBean>();
- for (int i = 0; i < pathList.Count; i++)
- {
- string pathName = pathList[i];
- List<FileBean> tepList = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
- if (tepList != null && tepList.Count > 0)
- {
- list.Add(tepList[0]);
- }
- }
- ShowByListFileBean(list);
- }
- /// <summary>
- /// 通过文件集合(图片)显示出来。
- /// </summary>
- /// <param name="list"></param>
- public void ShowByListFileBean(List<FileBean> list)
- {
- if (list == null) return;
- j = 0;
- i = 0;
- ing.Clear();
- ingname.Clear();
- this.list = list;
- ClearTmp();
- foreach (FileBean bean in list)
- {
- Image image;
- try
- {
- image = FileHelper.BytesToBitmap(bean.getFile());
- }
- catch
- {
- image = null;
- WriteBytesToTmp(bean.getFile(), bean.getFileName());
- }
- ing.Add(image);
- ingname.Add(bean.getFileName());
- if (ing[0] != null)
- {
- PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[0], pictureBox1);
- }
- this.currentFileName = list[0].getFileName();
- if (FileChangeEvent != null)
- {
- FileChangeEvent(currentFileName);
- }
- j = j + 1;
- }
- if (ing.Count == 0)
- {
- button1.Visible = false;
- button2.Visible = false;
- fal = false;
- this.pictureBox1.Image = null;
- this.currentFileName = "";
- if (FileChangeEvent != null)
- {
- FileChangeEvent(currentFileName);
- }
- // MessageBox.Show("文件夹是空的", "提示"); LX 修改
- label1.Text = "0/0";
- }
- else
- {
- button1.Visible = true;
- button2.Visible = true;
- fal = true;
- label1.Text = "1/" + j;
- }
- ShowDontViewBnt();
- }
- private void WriteBytesToTmp(byte[] Bytes, string fileName)
- {
- try
- {
- string tmpPath = Environment.CurrentDirectory + "\\Tmp\\";
- FileStream fs = new FileStream(tmpPath + fileName, FileMode.Create);
- fs.Write(Bytes, 0, Bytes.Length);
- fs.Flush();
- fs.Close();
- }
- catch { }
- }
- private void ClearTmp()
- {
- string tmpPath = Environment.CurrentDirectory + "\\Tmp\\";
- DirectoryInfo di = new DirectoryInfo(tmpPath);
- if (di.Exists == fal)
- {
- di.Create();
- return;
- }
- foreach (FileInfo fi in di.GetFiles())
- {
- try
- {
- fi.Delete();
- }
- catch { continue; }
- }
- }
- private void button2_Click(object sender, EventArgs e)//上一张
- {
- if (fal)
- {
- this.pictureBox1.Image = null;
- if (i == 0)
- {
- i = j - 1;
- if (ing[i] != null)
- {
- PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
- }
- this.currentFileName = list[i].getFileName();
- label1.Text = i + 1 + "/" + j;
- }
- else
- {
- i = i - 1;
- if (ing[i] != null)
- {
- PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
- }
- this.currentFileName = list[i].getFileName();
- label1.Text = i + 1 + "/" + j;
- }
- if (FileChangeEvent != null)
- {
- FileChangeEvent(currentFileName);
- }
- ShowDontViewBnt();
- }
- }
- private void button1_Click(object sender, EventArgs e)//下一张
- {
- if (fal)
- {
- this.pictureBox1.Image = null;
- if (i + 1 == j)
- {
- i = 0;
- if (ing[i] != null)
- {
- PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
- }
- this.currentFileName = list[i].getFileName();
- label1.Text = i + 1 + "/" + j;
- }
- else
- {
- i = i + 1;
- if (ing[i] != null)
- {
- PictureBoxHelper.LoadThumbnailImage((Bitmap)ing[i], pictureBox1);
- }
- this.currentFileName = list[i].getFileName();
- label1.Text = i + 1 + "/" + j;
- }
- if (FileChangeEvent != null)
- {
- FileChangeEvent(currentFileName);
- }
- ShowDontViewBnt();
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (ingname.Count == 0)
- {
- MessageUtil.ShowWarning("没有可删除的文件!");
- return;
- }
- if (MessageBox.Show("是否确定要进行删除操作", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) //LX 修改
- {
- bool isSuccess = Core.Mes.Client.Comm.Server.FileHelper.Delete(spathName + "/" + ingname[i] + ""); ;
- if (isSuccess)
- {
- MessageUtil.ShowTips("删除成功!");
- FlileDown(spathName);
- }
- }
- }
- private void ShowDontViewBnt()
- {
- if (this.pictureBox1.Image == null && ing.Count > 0)
- {
- label2.Visible = true;
- button4.Visible = true;
- label2.Text = "无法浏览文件:" + ingname[i];
- }
- else
- {
- label2.Visible = false;
- button4.Visible = false;
- }
- }
- private void pictureBox1_SizeChanged(object sender, EventArgs e)
- {
- if (this.DesignMode == true) return;
- if (_lbl2X == 0) return;
- label2.Location = new Point((int)((double)pictureBox1.Width * _lbl2X),
- (int)((double)pictureBox1.Height * _lbl2Y));
- button4.Location = new Point(label2.Location.X + 45, label2.Location.Y + 35);
- }
- private void panel1_SizeChanged(object sender, EventArgs e)
- {
- if (this.DesignMode == true) return;
- if (_btn1 == 0) return;
- button1.Location = new Point((int)((double)panel1.Width * _btn1), button1.Location.Y);
- button2.Location = new Point((int)((double)panel1.Width * _btn2), button1.Location.Y);
- button3.Location = new Point((int)((double)panel1.Width * _btn3), button1.Location.Y);
- button5.Location = new Point(button3.Location.X - 103, button3.Location.Y);
- }
- private void button4_Click(object sender, EventArgs e)
- {
- string filePath = Environment.CurrentDirectory + "\\Tmp\\" + ingname[i];
- Process.Start(filePath);
- }
- private void button5_Click(object sender, EventArgs e)
- {
- if (ing.Count == 0)
- {
- MessageUtil.ShowWarning("没有可下载的文件!");
- return;
- }
- if (batchFlag == "0")
- {
- SaveFileDialog file = new SaveFileDialog();
- string fileType = list[i].getFileName().Split('.')[list[i].getFileName().Split('.').Length - 1];
- file.Filter = "Document(*." + fileType + ")| *." + fileType;
- file.FileName = list[i].getFileName();
- DialogResult drStat = file.ShowDialog();
- if (drStat == DialogResult.OK)
- {
- FileStream fs = new FileStream(file.FileName, FileMode.Create);
- byte[] Bytes = list[i].getFile();
- fs.Write(Bytes, 0, Bytes.Length);
- fs.Flush();
- fs.Close();
- }
- }
- else if (batchFlag == "1")
- {
- FolderBrowserDialog fbd = new FolderBrowserDialog();
- fbd.ShowNewFolderButton = true;
- DialogResult drStat = fbd.ShowDialog();
- if (drStat == DialogResult.OK)
- {
- string path = fbd.SelectedPath;
- for (int index = 0; index < list.Count; index++)
- {
- string fileName = list[index].getFileName();
- string pathNew = path + "\\" + fileName;
- FileStream fs = new FileStream(pathNew, FileMode.Create);
- byte[] Bytes = list[index].getFile();
- fs.Write(Bytes, 0, Bytes.Length);
- fs.Flush();
- fs.Close();
- }
- if (FileCloseEvent != null)
- FileCloseEvent(path);
- }
- }
- }
- }
- }
|