c# 实现文件上传下载功能的实例代码
作者:少年。 发布时间:2021-12-10 15:00:30
标签:c#,文件,上传,下载
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("下载成功");
}
}
}
来源:https://www.cnblogs.com/ouyangkai/p/12484260.html


猜你喜欢
- 核心代码迁移相对顺利,大致流程如下:一、创建项目 1) cd cocos2d-x-3.0rc0;&nbs
- 1. Java对象结构Java对象结构包括三部分:对象头、对象体和填充字节,如图所示:对象头又包括三个字段:第一个字段叫作Mark Word
- 一、基本概念JavaWeb里面的listener是通过观察者设计模式进行实现的。对于观察者模式,这里不做过多介绍,大概讲一下什么意思。观察者
- 概述什么是 Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发框架. 要
- 背景项目中要实现横向列表的无限循环滚动,自然而然想到了RecyclerView,但我们常用的RecyclerView是不支持无限循环滚动的,
- 桥接模式桥接模式是将抽象部分与它的实现部分分离,使他们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)
- 本文为大家分享了Java实现文件上传下载功能的具体代码,供大家参考,具体内容如下前端通过form表单的enctype属性,将数据传递方式修改
- 日期和时间,在我们开发中非常重要。DateTime在C#中,专门用来表达和处理日期和时间。本文算是多年使用DateTime的一个总结,包括D
- 顺序结构我们之前写的大多代码都是顺序结构的,即按照代码的顺序一行一行的执行代码1 System.out.println("you&
- 在很多web产品中都需要实现在同一时刻,只能允许一个账号同时只能在一个浏览器当中登录。通俗点讲就是当A账号在浏览器1当中登录了,此时在浏览器
- SpringBoot找不到映射文件org.apache.ibatis.binding.BindingException: Invalid b
- 本文实例讲述了Android编程实现给Button添加图片和文字的方法。分享给大家供大家参考,具体如下://为按钮添加图片和文字的方法pub
- 1.概述在本快速教程中,我们将演示如何在Spring Boot应用程序中自定义Spring Security的身份验证失败处理。目标是使用表
- maven配置阿里云镜像打开maven配置文件,找到标签,添加如下:<mirrors> <mirror>
- 前言面向切面编程,利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发
- 前言之前在SpringBoot项目中简单使用定时任务,不过由于要借助cron表达式且都提前定义好放在配置文件里,不能在项目运行中动态修改任务
- 站点IP访问频率限制 针对单个站点using System;using System.Collections.Generic;u
- 问题描述Flutter 应用在 Android 端上启动时会有一段很明显的白屏现象,白屏的时长由设备的性能决定,设备性能越差,白屏时间越长。
- 本文实例分析了C#中登录窗体和欢迎窗体关闭方法。分享给大家供大家参考。具体分析如下:在c#的winform编程中,我们经常会做登录窗体或欢迎
- GUID是一个128位长的数字,一般用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID。从理论上讲,如果一台