c#文件上傳下載功能實現


c#文件上傳下載功能實現

NuGet 安裝SqlSugar

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 + "rn" +
                                  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表的常用操作
    }

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; }
    }

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;
        }
    }

窗體加載

讀取到數據庫字段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;

        }

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];
            }
        }

上傳事件觸發

  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);
            }
        }

下載事件觸發

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM