#region
using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Interfaces;
using CSFrameworkV5.Models;
#endregion
namespace CSFrameworkV5.Business
{
public class AttachmentTool
{
///
/// 由资料行的FileType(文件类型)字段创建图标
///
/// 资料行
/// 返回的大图标
/// 返回的小图标
///
public static void CreateFileIcon(DataRow sourceRow, out Icon large,
out Icon small, bool writeRow)
{
var fileType =
ConvertEx.ToString(sourceRow[tb_AttachFile.FileType]);
IconTool.CreateFileIcon(fileType, out large, out small);
if (writeRow)
{
var state = sourceRow.RowState;
if (large != null)
{
var ms1 = new MemoryStream();
large.ToBitmap().Save(ms1, ImageFormat.Bmp);
sourceRow[tb_AttachFile.IconLarge] = ms1.ToArray();
ms1.Close();
}
if (small != null)
{
var ms2 = new MemoryStream();
small.ToBitmap().Save(ms2, ImageFormat.Bmp);
sourceRow[tb_AttachFile.IconSmall] = ms2.ToArray();
ms2.Close();
}
if (state == DataRowState.Unchanged) sourceRow.AcceptChanges();
}
}
}
///
/// 将附件存储在SQLServer
///
public class AttachmentStorage_SQL : IAttachmentStorage
{
///
/// 附件数据表
///
private DataTable _attachmentStorage;
public AttachmentStorage_SQL()
{
}
public AttachmentStorage_SQL(DataTable attachmentStorage)
{
_attachmentStorage = attachmentStorage;
DoPrepareDataSource();
}
///
/// 创建临时文件
///
/// 文件数据
/// 文件名
///
private string CreateTempFile(byte[] fileBuffer, string fileName)
{
if (fileBuffer == null || fileBuffer.Length == 0)
throw new CustomException("文件内容为空,无法打开文件!");
var path = Path.GetTempPath() + @"\" + fileName;
if (File.Exists(path)) File.Delete(path); //删除旧文件
var fs = new FileStream(path, FileMode.OpenOrCreate);
fs.Write(fileBuffer, 0, fileBuffer.Length);
fs.Flush();
fs.Close();
return path;
}
///
/// 打开文件
///
/// 当前选择的文件
private void DoOpenFile(object attachmentDataRow)
{
try
{
var R = attachmentDataRow as DataRow;
var file = (byte[])R[tb_AttachFile.FileBody]; //取文件内容
var tempFile = CreateTempFile(file,
R[tb_AttachFile.FileName].ToStringEx());
var extension = Path.GetExtension(tempFile);
if (".jpg,.png,.bmp,.pdf,.doc,.docx,.xls,.xlsx,.txt,.zip,.rar"
.IndexOf(extension) >= 0)
{
var p = Process.Start(tempFile); //打开文件
if (p != null) p.WaitForExit();
File.Delete(tempFile);
}
else
{
throw new Exception("不支持的文件格式!");
}
}
catch (Exception ex)
{
Msg.ShowException(ex, "无法打开附件, 没有找到可以打开附件的程序!");
}
}
///
/// 准备数据源用于表格显示,主要功能:自动生成显示图标。
///
public void DoPrepareDataSource()
{
if (_attachmentStorage == null) return;
//临时显示字段
if (_attachmentStorage.Columns.IndexOf(tb_AttachFile.IconLarge) < 0)
_attachmentStorage.Columns.Add(tb_AttachFile.IconLarge,
typeof(byte[]));
if (_attachmentStorage.Columns.IndexOf(tb_AttachFile.IconSmall) < 0)
_attachmentStorage.Columns.Add(tb_AttachFile.IconSmall,
typeof(byte[]));
Icon large;
Icon small;
foreach (DataRow R in _attachmentStorage.Rows)
{
if (R.RowState == DataRowState.Deleted) continue;
if (R[tb_AttachFile.IconSmall] != DBNull.Value) continue;
AttachmentTool.CreateFileIcon(R, out large, out small,
true); //创建图标,用于表格显示
}
}
#region IAttachmentStorage Members
public DataTable AttachmentStorage
{
get => _attachmentStorage;
set
{
_attachmentStorage = value;
DoPrepareDataSource();
}
}
public void Save()
{
}
private void DeleteAll()
{
foreach (DataRow R in _attachmentStorage.Rows) R.Delete();
}
private byte[] GetFile(string fileName)
{
var rows = _attachmentStorage.Select("FileName='" + fileName + "'");
if (rows.Length > 0) return (byte[])rows[0][tb_AttachFile.FileBody];
return null;
}
///
/// 调用相关程序打开附件
///
public void OpenFile(string fileName)
{
var rows = _attachmentStorage.Select("FileName='" + fileName + "'");
if (rows.Length > 0)
{
//调用线程打开文件
var td = new Thread(DoOpenFile);
td.Start(rows[0]);
}
else
{
Msg.Warning("没有选择附件!");
}
}
public void DeleteFile(string fileName)
{
var rows = _attachmentStorage.Select("FileName='" + fileName + "'");
if (rows.Length > 0) rows[0].Delete();
}
public bool Exists(string fileName)
{
var rows = _attachmentStorage.Select("FileName='" + fileName + "'");
return rows.Length > 0;
}
public void SaveAs(string fileName)
{
var rows = _attachmentStorage.Select("FileName='" + fileName + "'");
if (rows.Length > 0)
try
{
var R = rows[0];
var file = (byte[])R["FileBody"]; //取文件内容
var tempFile =
CreateTempFile(file, R["FileName"].ToStringEx());
var dlg = new SaveFileDialog();
dlg.FileName = R["FileName"].ToStringEx();
if (DialogResult.OK == dlg.ShowDialog())
{
File.Copy(tempFile, dlg.FileName, true);
Msg.ShowInformation("保存成功!文件:" + dlg.FileName);
}
File.Delete(tempFile);
}
catch
{
Msg.Warning("无法打开附件, 没有找到可以打开附件的程序!");
}
else
Msg.Warning("没有选择附件!");
}
public void AddFile(object file)
{
var f = file as TAttachFile;
var row = _attachmentStorage.NewRow();
row[tb_AttachFile.DocID] = "";
row[tb_AttachFile.FileTitle] = f.FileTitle;
row[tb_AttachFile.FileName] = f.FileName;
row[tb_AttachFile.FileType] = f.FileType;
row[tb_AttachFile.FileSize] = f.FileSize;
row[tb_AttachFile.FileBody] = f.FileBody;
row[tb_AttachFile.IsDroped] = f.IsDroped;
row[tb_AttachFile.IconLarge] = f.IconLarge;
row[tb_AttachFile.IconSmall] = f.IconSmall;
_attachmentStorage.Rows.Add(row);
}
public void AddFile(string fileFullName)
{
FileStream fs = null;
try
{
var ext = Path.GetExtension(fileFullName); //扩展名
var fileName = Path.GetFileName(fileFullName); //显示文件名称
fs = new FileStream(fileFullName, FileMode.Open);
var bs = new byte[fs.Length]; //文件内容
fs.Read(bs, 0, (int)fs.Length); //读取文件内容
var size =
decimal.Round((decimal)(fs.Length / 1024.00),
2); //文件大小 kb
fs.Close();
//显示文件的图标
Icon large;
Icon small;
IconTool.CreateFileIcon(ext, out large, out small); //获取文件的图标
var file = new TAttachFile(); //附件对象
file.FileName = fileName;
file.FileTitle = fileName; //文件标题
file.FileType = ext; ////扩展名
file.FileSize = size;
file.FileBody = bs;
if (large != null)
file.IconLarge =
IconTool.ImageToByte(large.ToBitmap()); //大图标
if (small != null)
file.IconSmall =
IconTool.ImageToByte(small.ToBitmap()); //小图标
AddFile(file); //保存数据
}
catch
{
if (fs != null) fs.Close();
}
}
public void UpdateFile(object file)
{
var f = file as TAttachFile;
var rows =
_attachmentStorage.Select("FileID=" +
f.FileID.ToStringEx()); //查找旧记录
if (rows.Length > 0)
{
var row = rows[0];
row[tb_AttachFile.DocID] = "";
row[tb_AttachFile.FileTitle] = f.FileTitle;
row[tb_AttachFile.FileName] = f.FileName;
row[tb_AttachFile.FileType] = f.FileType;
row[tb_AttachFile.FileSize] = f.FileSize;
row[tb_AttachFile.FileBody] = f.FileBody;
row[tb_AttachFile.IsDroped] = f.IsDroped;
row[tb_AttachFile.IconLarge] = f.IconLarge;
row[tb_AttachFile.IconSmall] = f.IconSmall;
}
}
#endregion
}
///
/// 将附件存在共享目录
///
public class AttachmentStorage_Folder : IAttachmentStorage
{
#region IAttachmentStorage Members
public DataTable AttachmentStorage { get; set; }
public void Save()
{
}
public void DeleteAll()
{
}
public byte[] GetFile(string fileName)
{
return null;
}
public void OpenFile(string fileName)
{
}
public void DeleteFile(string fileName)
{
}
public bool Exists(string fileName)
{
return false;
}
public void AddFile(object file)
{
}
public void AddFile(string fileFullName)
{
}
public void UpdateFile(object file)
{
}
public void SaveAs(string fileName)
{
}
#endregion
}
///
/// 对象工厂
///
public class AttachmentFactory
{
public IAttachmentStorage CreateEmptyStorage()
{
return new AttachmentStorage_SQL();
}
}
}