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.Mes.Client.Comm.Control
{
public delegate void FileChange(string fileName);
///
/// 关闭窗体并传出文件夹
///
///
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;
///
/// 是否批量下载 默认否
///
private string batchFlag = "0";
public string BatchFlag
{
get { return batchFlag; }
set { batchFlag = value; }
}
private List folderList = new List();
public List FolderList
{
get { return folderList; }
set { folderList = value; }
}
public void SetPathList(List list)
{
FlileDownBatch(list);
}
public void SetPathList(List list, string batchFlag, List FolderList)
{
FlileDownBatch(list, batchFlag, FolderList);
}
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 ing = new List();
private ArrayList ingname = new ArrayList();
private List list = new List();
private string currentFileName = "";
public event FileChange FileChangeEvent;
public event FileClose FileCloseEvent;
public List 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 list = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
ShowByListFileBean(list);
}
///
/// 批量下载文件
///
///
public void FlileDownBatch(List pathList)
{
if (this.DesignMode) return;
if (pathList == null || pathList.Count <= 0) return;
List list = new List();
for (int i = 0; i < pathList.Count; i++)
{
string pathName = pathList[i];
List tepList = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
if (tepList != null && tepList.Count > 0)
{
list.Add(tepList[0]);
}
}
ShowByListFileBean(list);
}
public void FlileDownBatch(List pathList, string batchFlag, List FolderList)
{
if (this.DesignMode) return;
if (pathList == null || pathList.Count <= 0) return;
List list = new List();
for (int i = 0; i < pathList.Count; i++)
{
string[] a = pathList[i].Split(';');
string pathName = a[1];
List tepList = Core.Mes.Client.Comm.Server.FileHelper.Download(pathName);
if (tepList != null && tepList.Count > 0)
{
if (batchFlag == "2")
{
for (int j = 0; j < tepList.Count;j++ )
{
tepList[j].setPathName("/");
list.Add(tepList[j]);
FolderList.Add(a[0]);
}
}
else {
list.Add(tepList[0]);
FolderList.Add(a[0]);
}
}
}
ShowByListFileBean(list);
}
///
/// 通过文件集合(图片)显示出来。
///
///
public void ShowByListFileBean(List 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);
}
}
else if (batchFlag == "2")
{
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 folderName = folderList[index];
string fileName = list[index].getFileName();
if (!Directory.Exists(path + "\\" + folderName + "\\" + list[index].getPathName()))
{
Directory.CreateDirectory(path + "\\" + folderName + "\\" + list[index].getPathName());
}
string pathNew = path + "\\" + folderName + "\\" + list[index].getPathName() + "\\" + 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);
}
}
}
}
}