定义WebClient使用的操作类: 操作类名称WebUpDown

WebClient上传文件至Ftp服务:

//// <summary>

/// WebClient上传文件至Ftp服务

/// </summary>

/// <param name="fileNamePath">文件名,全路径格式</param>

/// <param name="uriString">服务器文件夹路径</param>

public static void UpLoadFile(string fileNamePath, string uriString)

{

string NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));

uriString = uriString + NewFileName;

//创建WebClient实例

WebClient myWebClient = new WebClient();

//指定用户名和密码

myWebClient.Credentials = new NetworkCredential("username", "password");

try

{

//上传文件

myWebClient.UploadFile(new Uri(uriString), fileNamePath);

}

catch (Exception ex)

{

MessageBox.Show("文件上传失败,失败原因:" + ex.Message);

}

finally

{

myWebClient.Dispose();

}

}

下载服务器文件至客户端:

/// <summary>

/// 下载服务器文件至客户端

/// </summary>

/// <param name="URL">被下载的文件地址,绝对路径</param>

/// <param name="Dir">另存放的目录</param>

public static void Download(string URL, string Dir)

{

WebClient client = new WebClient();

client.Credentials = new NetworkCredential("username "," password ");

string Path = Dir;   //另存为的绝对路径+文件名

try

{

client.DownloadFile(new Uri(URL), Path);

}

catch (Exception ex)

{

MessageBox.Show("文件下载失败,失败原因:" + ex.Message);

}

finally

{

client.Dispose();

}

}

调用方法:

/// <summary>

/// WebClient上传到Ftp服务

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Button_Click_5(object sender, RoutedEventArgs e)

{

WebUpDown.UpLoadFile(@"C:\123.txt",@"ftp://localhost//");

}

/// <summary>

/// WebClient使用Ftp服务下载到客户端

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Button_Click_6(object sender, RoutedEventArgs e)

{

WebUpDown.Download(@"ftp://localhost//123.txt", @"C:\123.txt");

}

webclient上传下载文件的更多相关文章

  1. WebClient上传下载文件,小白篇

    WebClient的上传文件一直报错,各种百度各种稀奇古怪的东西,终于百度到一篇小白学习篇 转自: https://www.cnblogs.com/cncc/p/5722231.html 使用C#We ...

  2. C#实现http协议支持上传下载文件的GET、POST请求

    C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...

  3. rz和sz上传下载文件工具lrzsz

    ######################### rz和sz上传下载文件工具lrzsz ####################################################### ...

  4. WebClient上传音频文件

    //WebClient上传音频文件 public string UploadVoice(string fileNamePath) { Voice model=new Voice(); string s ...

  5. linux上很方便的上传下载文件工具rz和sz

    linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...

  6. shell通过ftp实现上传/下载文件

    直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...

  7. SFTP远程连接服务器上传下载文件-qt4.8.0-vs2010编译器-项目实例

    本项目仅测试远程连接服务器,支持上传,下载文件,更多功能开发请看API自行开发. 环境:win7系统,Qt4.8.0版本,vs2010编译器 qt4.8.0-vs2010编译器项目实例下载地址:CSD ...

  8. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  9. HttpClient上传下载文件

    HttpClient上传下载文件 java HttpClient Maven依赖 <dependency> <groupId>org.apache.httpcomponents ...

随机推荐

  1. 高阶函数 filter map reduce

    const app=new Vue({ el:'#app', data:{ books:[{ id:1, name:"算法导论", data: '2006-1', price:39 ...

  2. [转帖]新手必读,16个概念入门 Kubernetes

    新手必读,16个概念入门 Kubernetes https://www.kubernetes.org.cn/5906.html 2019-09-29 22:13 中文社区 分类:Kubernetes教 ...

  3. PAT甲级 树 相关题_C++题解

    树 目录 <算法笔记>重点摘要 1004 Counting Leaves (30) 1053 Path of Equal Weight (30) 1079 Total Sales of S ...

  4. SecureCRT SSH 失败 Key exchange failed 解决方法

    背景:SecureCRT 的SSH正常使用过程中,突然出现: Key exchange failed. No compatible hostkey.The server supports these ...

  5. 【搜索+set去重】Balance Scale

    Balance Scale 题目描述 You, an experimental chemist, have a balance scale and a kit of weights for measu ...

  6. SpringBoot 第一篇:HelloWorld 跑起来

    背景 金融行业从业快十年,作为银行系开发人员来说开源框架了解不多非常正常,因为银行系的运行平台,基本上不会采购小厂商集合开源框架自建的产品,竞标的产品没有几十个成功案例,你也进不了这个门槛(有关系的除 ...

  7. Tree Generator™ CodeForces - 1149C (线段树,括号序列)

    大意: 给定括号序列, 每次询问交换两个括号, 求括号树的直径. 用[ZJOI2007]捉迷藏的方法维护即可. #include <iostream> #include <algor ...

  8. Optional 理解

    目录 Optional 理解 1. 含义 2. Optional 类中方法 3. Optional 对象不应该作为方法参数 Optional 理解 1. 含义 Optional 是一个容器对象,该容器 ...

  9. (二)Redis之Jedis概念和HelloWorld实现以及JedisPool的使用

    一.Jedis概念 实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis, 对于主流语言,Redis都提供了对应的客户端: 官网:https://redis.io/clients ...

  10. JDBC 复习3 存取Oracle大数据 clob blob

    1 目录结构记得导包咯 mysql oracle 2 代码,DBUtil工具类见前面的随笔博文 package dbex.mysql; import java.io.BufferedReader; i ...