1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#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
    {
        /// <summary>
        ///     由资料行的FileType(文件类型)字段创建图标
        /// </summary>
        /// <param name="sourceRow">资料行</param>
        /// <param name="large">返回的大图标</param>
        /// <param name="small">返回的小图标</param>
        /// <param name="writeRow"></param>
        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();
            }
        }
    }
 
    /// <summary>
    ///     将附件存储在SQLServer
    /// </summary>
    public class AttachmentStorage_SQL : IAttachmentStorage
    {
        /// <summary>
        ///     附件数据表
        /// </summary>
        private DataTable _attachmentStorage;
 
        public AttachmentStorage_SQL()
        {
        }
 
        public AttachmentStorage_SQL(DataTable attachmentStorage)
        {
            _attachmentStorage = attachmentStorage;
 
            DoPrepareDataSource();
        }
 
        /// <summary>
        ///     创建临时文件
        /// </summary>
        /// <param name="fileBuffer">文件数据</param>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        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;
        }
 
        /// <summary>
        ///     打开文件
        /// </summary>
        /// <param name="attachmentDataRow">当前选择的文件</param>
        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, "无法打开附件, 没有找到可以打开附件的程序!");
            }
        }
 
        /// <summary>
        ///     准备数据源用于表格显示,主要功能:自动生成显示图标。
        /// </summary>
        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;
        }
 
        /// <summary>
        ///     调用相关程序打开附件
        /// </summary>
        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
    }
 
    /// <summary>
    ///     将附件存在共享目录
    /// </summary>
    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
    }
 
    /// <summary>
    ///     对象工厂
    /// </summary>
    public class AttachmentFactory
    {
        public IAttachmentStorage CreateEmptyStorage()
        {
            return new AttachmentStorage_SQL();
        }
    }
}