关于c#:c文件上传下载功能实现

7次阅读

共计 3536 个字符,预计需要花费 9 分钟才能阅读完成。

NuGet 装置 SqlSugar

Model 文件下新建 DbContext 类

public class DbContext 
{public DbContext() 
    {Db = new SqlSugarClient(new ConnectionConfig() 
        {
            ConnectionString = "server=localhost;uid=root;pwd=woshishui;database=test",
                            DbType = DbType.MySql,
                            InitKeyType = InitKeyType.Attribute,// 从个性读取主键和自增列信息
            IsAutoCloseConnection = true,// 开启主动开释模式和 EF 原理一样我就不多解释了
            });
            // 调式代码 用来打印 SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
                        {
                            Console.WriteLine(sql + "rn" +
                                  Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();};
        }
        // 留神:不能写成动态的,不能写成动态的
        public SqlSugarClient Db;// 用来处理事务多表查问和简单的操作
        public SimpleClient<uploading> uploadingdb {get { return new SimpleClient<uploading>(Db); } }// 用来解决 Student 表的罕用操作
    }

uploading 实体类

[SugarTable("uploading")]
   public class uploading
    {
        // 指定主键和自增列,当然数据库中也要设置主键和自增列才会无效
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int id {get; set;}
        public string name {get; set;}
        public string path {get; set;}
    }

UploadingManager

 class UploadingManager : DbContext
    {public List<uploading> Query()
        {
            try
            {List<uploading> data = Db.Queryable<uploading>()
                    .Select(f => new uploading
                    {
                        name = f.name,
                        path = f.path
                    })
                    .ToList();
                return data;
            }
            catch (Exception e)
            {Console.WriteLine(e);
                throw;
            }
        }

        public List<string> GetName(string name)
        {List<string> data = Db.Queryable<uploading>()
                .Where(w=>w.name== name)
                .Select(f => f.path)
                .ToList();
            return data;
        }
    }

窗体加载

读取到数据库字段 name 并赋值

 private void Form1_Load(object sender, EventArgs e)
        {List<uploading> data = uploading.Query();
            foreach (var data1 in data)
            {comboBox1.Items.Add(data1.name);
            }
            comboBox1.SelectedIndex = 0;

        }

comboBox 事件触发条件查问到上传的 path

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {List<string> data = uploading.GetName(comboBox1.Text);

            for (int i = 0; i < data.Count; i++)
            {textBox1.Text = data[0];
            }
        }

上传事件触发

  private void Button1_Click(object sender, EventArgs e)
        {
             string path = textBox1.Text;
            CopyDirs(textBox3.Text,
                path);
        }
 private void CopyDirs(string srcPath, string aimPath)
        {
            try
            {
                // 查看目标目录是否以目录宰割字符完结如果不是则增加
                if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                {aimPath += System.IO.Path.DirectorySeparatorChar;}
                // 判断目标目录是否存在如果不存在则新建
                if (!System.IO.Directory.Exists(aimPath))
                {System.IO.Directory.CreateDirectory(aimPath);
                }
                // 失去源目录的文件列表,该外面是蕴含文件以及目录门路的一个数组
                // 如果你指向 copy 指标文件上面的文件而不蕴含目录请应用上面的办法
                // string[] fileList = Directory.GetFiles(srcPath);string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
                // 遍历所有的文件和目录
                foreach (string file in fileList)
                {
                    // 先当作目录解决如果存在这个目录就递归 Copy 该目录上面的文件
                    if (System.IO.Directory.Exists(file))
                    {CopyDir(file, aimPath + System.IO.Path.GetFileName(file));
                        DisplaylistboxMsg("上传胜利");
                    }
                    // 否则间接 Copy 文件
                    else
                    {System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true);
                        DisplaylistboxMsg("上传胜利");
                    }
                }
            }
            catch (Exception e)
            {DisplaylistboxMsg("上传失败" + e.Message);
            }
        }

下载事件触发

private void Button2_Click(object sender, EventArgs e)
        {CopyDir(@"\\10.55.2.3\mech_production_line_sharing\Test\" + textBox2.Text, textBox4.Text);
        }

private void CopyDir(string srcPath, string aimPath)
        {
            // 查看目标目录是否以目录宰割字符完结如果不是则增加
            if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
            {aimPath += System.IO.Path.DirectorySeparatorChar;}

            // 判断目标目录是否存在如果不存在则新建
            if (!System.IO.Directory.Exists(aimPath))
            {System.IO.Directory.CreateDirectory(aimPath);
            }

            // 失去源目录的文件列表,该外面是蕴含文件以及目录门路的一个数组
            // 如果你指向 copy 指标文件上面的文件而不蕴含目录请应用上面的办法
            // string[] fileList = Directory.GetFiles(srcPath);string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
            // 遍历所有的文件和目录
            foreach (string file in fileList)
            {
                // 先当作目录解决如果存在这个目录就递归 Copy 该目录上面的文件
                if (System.IO.Directory.Exists(file))
                {CopyDir(file, aimPath + System.IO.Path.GetFileName(file));
                    DisplaylistboxMsg("下载胜利");
                }
                // 否则间接 Copy 文件
                else
                {System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true);
                    DisplaylistboxMsg("下载胜利");
                }
            }
        }
正文完
 0