首页 > 编程语言 >c#文件上传下载功能实现

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

时间:2023-11-01 14:58:02浏览次数:24  
标签:文件 file string aimPath c# 上传下载 System IO public

NuGet 安装SqlSugar

1.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 + "\r\n" +
                                  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表的常用操作
    }

 

2.建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; }
    }

 

3.Manager文件下建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;

        }
    }

 

窗体加载Form1_Load

1.读取到数据库字段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;

        }

 

2.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];
            }
        }

 

3.上传事件触发

  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);
            }
        }

 

4.下载事件触发

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("下载成功");
                }
            }
        }
 

参考文章:http://blog.ncmem.com/wordpress/2023/11/01/c%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e4%b8%8b%e8%bd%bd%e5%8a%9f%e8%83%bd%e5%ae%9e%e7%8e%b0/

欢迎入群一起讨论

 

 

标签:文件,file,string,aimPath,c#,上传下载,System,IO,public
From: https://www.cnblogs.com/songsu/p/17803088.html

相关文章

  • Man or Honor 怒海潜将,壮志潜龙 美军的Navy Dive Carl Brashear
    上午路上刷到一个电影解说,讲的是CarlBrashear,从一位黑人少年,成长为美军中潜水不对MasterChief的传奇经历。人啊,凡事要靠自己,自我成长比什么都重要。剧中的那句ASNF-ASonNeverForgets,赤子之心,是发人深省的警句。......
  • 小白也能看懂的 AUC 曲线详解
    小白也能看懂的AUC曲线详解简介上篇文章小白也能看懂的ROC曲线详解介绍了ROC曲线。本文介绍AUC。AUC的全名为AreaUndertheROCCurve,即ROC曲线下的面积,最大为1。根据ROC和AUC的关系,我们可以得到如下结论ROC曲线接近左上角--->AUC接近1:模型预测准确......
  • 2022 CSPJ
     直接模拟即可,注意特判#include<iostream>#include<cstdio>#include<ctime>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#definemaxn200010#defineLLlonglongusingnamespacestd;intmain(){......
  • kserve cert-manager是什么关系?
    k8s的webhook就是给pod(或其他资源)修改一些东西的,像是pod的queue-proxy容器就是webhook给注入的。webhook什么时候被调用呢?apply后,请求到达api-server,api-server处理请求时,调用的webhook,调用webhook后,进入etcd。  api-server通过读取mutatingwebhookconfiguration和va......
  • Docker从了解到部署应用的详细教程
    一、Docker基础知识1、Docker(1)Docker可以让开发者打包他们的应用以及依赖包到一个轻量级、可以移植的容器中,然后发布到任何的linux机器上,可以实现虚拟化;(2)Docker容器是完全使用沙箱机制,相互之间不会有任何接口,更重要的是容器性能开销极低;2、Docker的应用场景(1)Web应用的自动化......
  • Oracle数据库常用命令
    数据库初始化命令表空间相关建立表空间--建立临时表空间CREATETEMPORARYTABLESPACEtemp_name--创建名为temp_name的临时表空间TEMPFILE'D:\app\super\oradata\oracle\temp_name.DBF'--临时文件SIZE50M--其初始大小为50MAUTOEXTENDON--支持自动扩展NEXT......
  • Exception in thread "main" java.net.BindException: Cannot assign requested addre
    两种情况1.端口号被占用,导致地址无法绑定#windows查看端口pidnetstat-aon|findstr8080(端口号)#linux查看端口占用netstat-anp|grep80802.ip地址与本机地址不匹配,导致地址无法绑定#windows查看ipipconfig#linux查看ipifconfig......
  • 线性代数 · 矩阵 · Matlab | Cholesky 分解代码实现
    (搬运外网的代码,非原创;原网址)(其实是专业课作业,但感觉国内博客没有合适的代码实现,所以就搬运到自己博客了)背景-Cholesky分解:若A为n阶实对称正定矩阵,则存在非奇异下三角矩阵L,使得A=LL^T。是特殊的LU分解(下三角上三角分解)。若限定L的对角元素为正,则这种分解......
  • 思科认证 | 2023年CCIE考试流程、费用、考场攻略!
    CCIE认证是通往网络通信领域专家之路的重要一步。想要在职业生涯中取得更多的成就吗?CCIE认证是不可或缺的助力。今天就给你说说CCIE考试流程详解,费用及考场攻略。012023年CCIE考试流程详解一、填写考生报名表在报名时,你需要提供一些必要的信息,包括考生姓名、E-MAIL地址、中英文联......
  • 网工内推 | 运维工程师,熟悉主流云平台,六险一金,RHCE优先
    01中核核信招聘岗位:数据中心运维工程师职责描述:1、负责云平台及数据中心基础设施的日常运维工作,提前发现云平台稳定性问题,确保问题及时发现并处理,提升客户核心业务的稳定性;2、负责数据中心基础设施设备运行状态监控和定期巡查工作,对设备健康检查、优化调试,提出改进建议,能在日常......