c# 实现文件上传下载功能的实例代码
NuGet安装SqlSugar
1.Model文件下新建DbContext类
publicclassDbContext { publicDbContext() { Db=newSqlSugarClient(newConnectionConfig() { 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(); }; } //注意:不能写成静态的,不能写成静态的 publicSqlSugarClientDb;//用来处理事务多表查询和复杂的操作 publicSimpleClient<uploading>uploadingdb{get{returnnewSimpleClient<uploading>(Db);}}//用来处理Student表的常用操作 }
2.建uploading实体类
[SugarTable("uploading")] publicclassuploading { //指定主键和自增列,当然数据库中也要设置主键和自增列才会有效 [SugarColumn(IsPrimaryKey=true,IsIdentity=true)] publicintid{get;set;} publicstringname{get;set;} publicstringpath{get;set;} }
3.Manager文件下建UploadingManager
classUploadingManager:DbContext { publicListQuery() { try { List data=Db.Queryable () .Select(f=>newuploading { name=f.name, path=f.path }) .ToList(); returndata; } catch(Exceptione) { Console.WriteLine(e); throw; } } publicList<string>GetName(stringname) { List<string>data=Db.Queryable<uploading>() .Where(w=>w.name==name) .Select(f=>f.path) .ToList(); returndata; } }
窗体加载Form1_Load
1.读取到数据库字段name并赋值
privatevoidForm1_Load(objectsender,EventArgse) { Listdata=uploading.Query(); foreach(vardata1indata) { comboBox1.Items.Add(data1.name); } comboBox1.SelectedIndex=0; }
2.comboBox事件触发条件查询到上传的path
privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse) { Listdata=uploading.GetName(comboBox1.Text); for(inti=0;i<data.Count;i++) { textBox1.Text=data[0]; } }
3.上传事件触发
privatevoidButton1_Click(objectsender,EventArgse) { stringpath=textBox1.Text; CopyDirs(textBox3.Text, path); }
privatevoidCopyDirs(stringsrcPath,stringaimPath) { 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(stringfileinfileList) { //先当作目录处理如果存在这个目录就递归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(Exceptione) { DisplaylistboxMsg("上传失败"+e.Message); } }
4.下载事件触发
privatevoidButton2_Click(objectsender,EventArgse) { CopyDir(@"\\10.55.2.3\mech_production_line_sharing\Test\"+textBox2.Text,textBox4.Text); } privatevoidCopyDir(stringsrcPath,stringaimPath) { //检查目标目录是否以目录分割字符结束如果不是则添加 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(stringfileinfileList) { //先当作目录处理如果存在这个目录就递归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("下载成功"); } } }
以上就是c#实现文件上传下载功能的实例代码的详细内容,更多关于c#实现文件上传下载的资料请关注毛票票其它相关文章!