文件附件列表框支持拖放文件

This commit is contained in:
Falcon 2024-10-08 11:04:23 +08:00
parent 62bd620580
commit 306e06045c
5 changed files with 219 additions and 54 deletions

View File

@ -0,0 +1,40 @@
namespace CalendarNotepad.Extends
{
/// <summary>
/// 文件列表视图模型
/// </summary>
public class ListViewFileModel
{
public ListViewItem Item { get; set; }
public ListViewFileModel() : this("","","初始化") { }
public ListViewFileModel(string? fileName,string? fileKey,string? status) {
var item = new ListViewItem(Path.GetFileName(fileName));
item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = status });
item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = fileKey });
this.Item = item;
}
public ListViewFileModel(ListViewItem item) {
this.Item = item;
}
public ListViewItem ToListViewItem() {
return this.Item;
}
/// <summary>
/// 文件名
/// </summary>
public string? FileName { get => this.Item.Name; set => this.Item.Name = Path.GetFileName(value); }
/// <summary>
/// 文件指纹
/// </summary>
public string? FileKey { get => this.Item.SubItems[2].Text; set => this.Item.SubItems[2].Text = value; }
/// <summary>
/// 文件说明
/// </summary>
public string? Status { get => this.Item.SubItems[1].Text; set => this.Item.SubItems[1].Text = value; }
}
}

View File

@ -1,4 +1,5 @@
using System.Security.Cryptography;
using CalendarNotepad.Models;
using System.Security.Cryptography;
using System.Text;
namespace CalendarNotepad.Extends
@ -62,5 +63,22 @@ namespace CalendarNotepad.Extends
bw.Flush();
bw.Close();
}
/// <summary>
/// 存储文件到数据库
/// </summary>
/// <param name="db">数据库上下文</param>
/// <param name="fullFilePath">完整文件名</param>
/// <returns>文件指纹</returns>
public static string SaveFileToDb(DbContext db,string fullFilePath) {
var fc = PlusFile.LoadFile(fullFilePath);
var fk = PlusFile.GetMd5(fc);
var pfu = new PlusFileUnit { FileContent = fc,FileId = fk };
var qu = db.Queryable<PlusFileUnit>().Where(a => a.FileId == fk);
if(qu.Any()) {
return qu.First().FileId;
}
db.Insertable(pfu).ExecuteCommand();
return fk;
}
}
}

View File

@ -56,11 +56,12 @@
tsb_loadin = new ToolStripButton();
FileListView = new ListView();
cms_FileList = new ContextMenuStrip(components);
ToolStripMenuItem = new ToolStripMenuItem();
toolStripSeparator2 = new ToolStripSeparator();
ToolStripMenuItem = new ToolStripMenuItem();
ToolStripMenuItem = new ToolStripMenuItem();
ToolStripMenuItem = new ToolStripMenuItem();
ToolStripMenuItem = new ToolStripMenuItem();
ToolStripMenuItem = new ToolStripMenuItem();
tabPage1 = new TabPage();
flowLayoutPanel5 = new FlowLayoutPanel();
bt_m_dbshc = new Button();
@ -78,7 +79,6 @@
panel2 = new Panel();
panel3 = new Panel();
OpenFileDlg = new OpenFileDialog();
toolStripSeparator2 = new ToolStripSeparator();
tabControl1.SuspendLayout();
tpWorkManage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
@ -418,50 +418,57 @@
FileListView.TabIndex = 9;
FileListView.UseCompatibleStateImageBehavior = false;
FileListView.View = View.Details;
FileListView.DragDrop += FileListView_DragDrop;
FileListView.DragEnter += FileListView_DragEnter;
FileListView.DoubleClick += FileListView_DoubleClick;
//
// cms_FileList
//
cms_FileList.Items.AddRange(new ToolStripItem[] { ToolStripMenuItem,toolStripSeparator2,ToolStripMenuItem,ToolStripMenuItem,ToolStripMenuItem,ToolStripMenuItem });
cms_FileList.Name = "contextMenuStrip1";
cms_FileList.Size = new Size(181,142);
cms_FileList.Size = new Size(101,120);
//
// 打开ToolStripMenuItem
//
ToolStripMenuItem.Font = new Font("微软雅黑",9F,FontStyle.Bold,GraphicsUnit.Point);
ToolStripMenuItem.Name = "打开ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(100,22);
ToolStripMenuItem.Text = "打开";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// toolStripSeparator2
//
toolStripSeparator2.Name = "toolStripSeparator2";
toolStripSeparator2.Size = new Size(97,6);
//
// 删除ToolStripMenuItem
//
ToolStripMenuItem.Name = "删除ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Size = new Size(100,22);
ToolStripMenuItem.Text = "删除";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// 导入ToolStripMenuItem
//
ToolStripMenuItem.Name = "导入ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Size = new Size(100,22);
ToolStripMenuItem.Text = "导入";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// 下载ToolStripMenuItem
//
ToolStripMenuItem.Name = "下载ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Size = new Size(100,22);
ToolStripMenuItem.Text = "下载";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// 更名ToolStripMenuItem
//
ToolStripMenuItem.Name = "更名ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Size = new Size(100,22);
ToolStripMenuItem.Text = "更名";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// 打开ToolStripMenuItem
//
ToolStripMenuItem.Font = new Font("微软雅黑",9F,FontStyle.Bold,GraphicsUnit.Point);
ToolStripMenuItem.Name = "打开ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Text = "打开";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// tabPage1
//
tabPage1.Controls.Add(flowLayoutPanel5);
@ -640,11 +647,6 @@
//
OpenFileDlg.Multiselect = true;
//
// toolStripSeparator2
//
toolStripSeparator2.Name = "toolStripSeparator2";
toolStripSeparator2.Size = new Size(177,6);
//
// WorkMain
//
AutoScaleDimensions = new SizeF(7F,17F);

View File

@ -37,6 +37,8 @@ namespace CalendarNotepad
this.FileListView.Columns.Add(new ColumnHeader {
Text = "文件指纹",Width = 100,TextAlign = HorizontalAlignment.Left,
});
this.FileListView.AllowDrop = true;
}
private void Rab_CheckedChanged(object? sender,EventArgs e) {
@ -281,25 +283,13 @@ namespace CalendarNotepad
}
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
foreach(var f in OpenFileDlg.FileNames) {
var lit1 = new ListViewItem(Path.GetFileName(f));
lit1.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "导入中" });
lit1.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "" });
this.FileListView.Items.Add(lit1);
var fc = PlusFile.LoadFile(f);
var fk = PlusFile.GetMd5(fc);
lit1.SubItems[2].Text = fk;
var pfu = new PlusFileUnit { FileContent = fc,FileId = fk };
var qu = this.Db.Queryable<PlusFileUnit>().Where(a => a.FileId == fk);
if(!qu.Any()) {
this.Db.Insertable(pfu).ExecuteCommand();
}
var fk = PlusFile.SaveFileToDb(this.Db,f);
this.Db.Insertable(new WorkUnitPlusFile {
FileKey = fk,
WorkDay = wd,
FileName = Path.GetFileName(f),
}).ExecuteCommand();
lit1.SubItems[1].Text = "已导入";
this.FileListView.Items.Add(new ListViewFileModel(f,fk,"已导入").ToListViewItem());
}
}
@ -444,6 +434,37 @@ namespace CalendarNotepad
ToolStripMenuItem_Click(sender,e);
}
private void FileListView_DragEnter(object sender,DragEventArgs e) {
if(e.Data == null) {
e.Effect = DragDropEffects.None;
return;
}
var isfd = e.Data.GetDataPresent(DataFormats.FileDrop);
if(isfd) {
e.Effect = DragDropEffects.Copy;
return;
}
e.Effect = DragDropEffects.None;
}
private void FileListView_DragDrop(object sender,DragEventArgs e) {
if(e.Data == null) {
return;
}
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
foreach(string file in files) {
MessageBox.Show(file);
var fk = PlusFile.SaveFileToDb(this.Db,file);
this.Db.Insertable(new WorkUnitPlusFile {
FileKey = fk,
WorkDay = wd,
FileName = Path.GetFileName(file),
}).ExecuteCommand();
this.FileListView.Items.Add(new ListViewFileModel(file,fk,"已导入").ToListViewItem());
}
}
/// <summary>
/// 当前记录日志的键
/// </summary>

View File

@ -132,6 +132,66 @@
<metadata name="C_WorkOut.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="btSave.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="btReset.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="gbWorkTypes.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="C_WorkTypes.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="mcWorkDay.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="splitContainer2.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="rtbMsg.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="toolStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="toolStrip1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="FileListView.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="cms_FileList.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>127, 17</value>
</metadata>
<metadata name="tabPage1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel5.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel4.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel3.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel2.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="panel2.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="panel3.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="C_WorkOut.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="radioButton9.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
@ -144,7 +204,16 @@
<metadata name="radioButton7.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<metadata name="radioButton9.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="radioButton6.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="radioButton8.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="radioButton7.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="btSave.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
@ -153,9 +222,6 @@
<metadata name="btReset.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="gbWorkTypes.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="C_WorkTypes.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
@ -174,19 +240,19 @@
<metadata name="radioButton5.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="mcWorkDay.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<metadata name="radioButton1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="splitContainer2.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<metadata name="radioButton3.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="rtbMsg.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<metadata name="radioButton2.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="toolStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
<metadata name="radioButton4.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="toolStrip1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<metadata name="radioButton5.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
@ -208,15 +274,6 @@
AAAAAElFTkSuQmCC
</value>
</data>
<metadata name="FileListView.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="cms_FileList.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>127, 17</value>
</metadata>
<metadata name="tabPage1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel5.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
@ -226,6 +283,12 @@
<metadata name="label5.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="bt_m_dbshc.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="label5.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel4.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
@ -235,6 +298,12 @@
<metadata name="label1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="bt_m_Backup.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="label1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel3.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
@ -247,6 +316,15 @@
<metadata name="lab_m_dbsize.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="bt_m_ys.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="label3.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="lab_m_dbsize.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="flowLayoutPanel2.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
@ -256,6 +334,12 @@
<metadata name="label2.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="bt_m_Clean.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="label2.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="panel2.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>