c# 实现文件上传下载功能的实例代码

 更新时间:2020年07月02日 11:39:55   作者:少年。  
这篇文章主要介绍了如何用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&lt;string&gt; GetName(string name)
  {
    List&lt;string&gt; data = Db.Queryable&lt;uploading&gt;()
      .Where(w=&gt;w.name== name)
      .Select(f =&gt; 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 &lt; 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(&quot;上传成功&quot;);
        }
        // 否则直接Copy文件
        else
        {
          System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true);
          DisplaylistboxMsg(&quot;上传成功&quot;);
        }
      }
    }
    catch (Exception e)
    {
      DisplaylistboxMsg(&quot;上传失败&quot; + 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(&quot;下载成功&quot;);
      }
      // 否则直接Copy文件
      else
      {
        System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true);
        DisplaylistboxMsg(&quot;下载成功&quot;);
      }
    }
  }

以上就是c# 实现文件上传下载功能的实例代码的详细内容,更多关于c# 实现文件上传下载的资料请关注脚本之家其它相关文章!

相关文章

  • 一文详解C#中重写(override)及覆盖(new)的区别

    一文详解C#中重写(override)及覆盖(new)的区别

    这篇文章主要为大家详细介绍了C#中重写(override)及覆盖(new)这两个关键词的区别,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-03-03
  • C#中的==运算符

    C#中的==运算符

    这篇文章主要介绍了C#中的==运算符,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-06-06
  • C# StringBuilder和string

    C# StringBuilder和string

    这篇文章主要介绍了C# StringBuilder和string,文章围绕StringBuilder和string的相关资料展开内容,需要的朋友可以参考一下
    2021-11-11
  • C# 中 “$” 符号的作用以及用法详解

    C# 中 “$” 符号的作用以及用法详解

    这篇文章主要介绍了C# 中 “$” 符号的作用以及用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • Unity UGUI的ToggleGroup选项组件介绍使用

    Unity UGUI的ToggleGroup选项组件介绍使用

    这篇文章主要为大家介绍了Unity UGUI的ToggleGroup选项组件介绍使用示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • c#的dllimport使用方法详解

    c#的dllimport使用方法详解

    DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息
    2014-01-01
  • C#实现QQ截图功能及相关问题

    C#实现QQ截图功能及相关问题

    这篇文章主要为大家详细介绍了C#实现QQ截图功能及相关问题,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • C#之IO读写文件方法封装代码

    C#之IO读写文件方法封装代码

    这篇文章主要用C#技术讲解了IO读写文件方法封装实例,有需要的朋友可以参考下
    2015-07-07
  • Unity UGUI实现滑动翻页直接跳转页数

    Unity UGUI实现滑动翻页直接跳转页数

    这篇文章主要为大家详细介绍了Unity UGUI实现滑动翻页,直接跳转页数,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • C#对WPF数据绑定的菜单插入Seperator分隔

    C#对WPF数据绑定的菜单插入Seperator分隔

    这篇文章介绍了C#对WPF数据绑定的菜单插入Seperator分隔的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06

最新评论