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
0
投稿
猜你喜欢
- 本文介绍了Spring Security 控制授权的方法,分享给大家,具体如下:使用授权方法进行授权配置每一个 Spring Securit
- The error simply says, “you've made changes in files in your works
- RelativePanel是在Windows 10 UWP程序中引入的一种新的布局面板,它是通过附加属性设置元素间的位置关系来对实现布局的。
- public static boolean isMobileNumber(String mobiles) {return Pattern.c
- 什么是sam 转换Single Abstract Method 实际上这是java8中提出的概念,你就把他理解为是 一个方法的接口 的就可以
- Redis是一个缓存消息中间件及具有丰富特性的键值存储系统。Spring Boot为Jedis客户端库和由Spring Data Redis
- 1. 定义TreeMap的排序方法使用Comparator对象作为参数需要注意的是:排序方法是针对键的,而不是值的。如果想针对值,需要更麻烦
- 多继承指一个子类能同时继承于多个父类,从而同时拥有多个父类的特征,但缺点是显著的。1.若子类继承的父类中拥有相同的成员变量,子类在引用该变量
- 一、同步问题提出线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏。例如:两个线程ThreadA、ThreadB都操作同一个对
- 在实际应用中,大家使用的密码可以说多种多样,但是无论有多少,其组成不遑是有可打印字符组成的,我们可以认为class CreateDic{ p
- 一、材料准备1、jdk1.8的安装包2、maven安装包3、idea工具二、配置jdk1.8环境变量1.jdk下载jdk下载网址(点击此链接
- 这篇文章主要介绍了SpringBoot下如何实现支付宝接口的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- String 对象是不可改变的。每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对
- 概述透视表是依据已有数据源来创建的交互式表格,我们可在excel中创建透视表,也可编辑已有透视表。所需工具:Free Spire.XLS f
- 1 自定义类加载器自定义类加载器的代码很简单,只需要继承ClassLoader类,覆写findClass方法即可其默认实现是会抛出一个异常:
- 业务现象代码中有一部分代码多次嵌套循环和数据处理,执行速度很慢解决方案通过多线程1、启用多线程private final static Ex
- 一、JTA组件简介什么是JTAJTA,全称:Java Transaction API。JTA事务比JDBC事务更强大。一个JTA事务可以有多
- 本文介绍了 SpringBoot之Controller的使用,分享给大家,具体如下:1.@Controller:处理http请求 2.@Re
- 一问道StringBuffer与StringBuilder的区别,张口就来StringBuffer是线程安全的,因为它相关方法都加了sync
- Memento定义:memento是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到原先保存的状态。Memento模式相