当前位置: 移动技术网 > IT编程>开发语言>c# > c#文件上传下载功能实现

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

2020年03月13日  | 移动技术网IT编程  | 我要评论

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

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网