c# 實(shí)現(xiàn)文件上傳下載功能的實(shí)例代碼
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原理一樣我就不多解釋了 }); //調(diào)式代碼 用來打印SQL Db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); Console.WriteLine(); }; } //注意:不能寫成靜態(tài)的,不能寫成靜態(tài)的 public SqlSugarClient Db;//用來處理事務(wù)多表查詢和復(fù)雜的操作 public SimpleClient<uploading> uploadingdb { get { return new SimpleClient<uploading>(Db); } }//用來處理Student表的常用操作 }
2.建uploading實(shí)體類
[SugarTable("uploading")] public class uploading { //指定主鍵和自增列,當(dāng)然數(shù)據(jù)庫中也要設(shè)置主鍵和自增列才會有效 [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.讀取到數(shù)據(jù)庫字段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事件觸發(fā)條件查詢到上傳的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.上傳事件觸發(fā)
private void Button1_Click(object sender, EventArgs e) { string path = textBox1.Text; CopyDirs(textBox3.Text, path); }
private void CopyDirs(string srcPath, string aimPath) { try { // 檢查目標(biāo)目錄是否以目錄分割字符結(jié)束如果不是則添加 if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar) { aimPath += System.IO.Path.DirectorySeparatorChar; } // 判斷目標(biāo)目錄是否存在如果不存在則新建 if (!System.IO.Directory.Exists(aimPath)) { System.IO.Directory.CreateDirectory(aimPath); } // 得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個數(shù)組 // 如果你指向copy目標(biāo)文件下面的文件而不包含目錄請使用下面的方法 // string[] fileList = Directory.GetFiles(srcPath); string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath); // 遍歷所有的文件和目錄 foreach (string file in fileList) { // 先當(dāng)作目錄處理如果存在這個目錄就遞歸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.下載事件觸發(fā)
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) { // 檢查目標(biāo)目錄是否以目錄分割字符結(jié)束如果不是則添加 if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar) { aimPath += System.IO.Path.DirectorySeparatorChar; } // 判斷目標(biāo)目錄是否存在如果不存在則新建 if (!System.IO.Directory.Exists(aimPath)) { System.IO.Directory.CreateDirectory(aimPath); } // 得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個數(shù)組 // 如果你指向copy目標(biāo)文件下面的文件而不包含目錄請使用下面的方法 // string[] fileList = Directory.GetFiles(srcPath); string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath); // 遍歷所有的文件和目錄 foreach (string file in fileList) { // 先當(dāng)作目錄處理如果存在這個目錄就遞歸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# 實(shí)現(xiàn)文件上傳下載功能的實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于c# 實(shí)現(xiàn)文件上傳下載的資料請關(guān)注腳本之家其它相關(guān)文章!
- c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn)
- C# FileStream實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳
- C# 文件下載之?dāng)帱c(diǎn)續(xù)傳實(shí)現(xiàn)代碼
- C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- C#實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳下載的方法
- c#實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能示例分享
- C#實(shí)現(xiàn)支持?jǐn)帱c(diǎn)續(xù)傳多線程下載客戶端工具類
- C#服務(wù)端圖片打包下載實(shí)現(xiàn)代碼解析
- C#怎樣實(shí)現(xiàn)文件下載斷點(diǎn)續(xù)傳
相關(guān)文章
CefSharp如何進(jìn)行頁面的縮放(Ctrl+滾輪)
CefSharp簡單來說就是一款.Net編寫的瀏覽器包,本文主要介紹了CefSharp如何進(jìn)行頁面的縮放,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06C#通過創(chuàng)建Windows服務(wù)啟動程序的方法詳解
這篇文章主要介紹了C#通過創(chuàng)建Windows服務(wù)啟動程序的方法,較為詳細(xì)的分析了C#創(chuàng)建Windows服務(wù)應(yīng)用程序的步驟與相關(guān)注意事項,需要的朋友可以參考下2016-06-06