一、WebRequestMethods.Ftp类:

表示可与 FTP 请求一起使用的 FTP 协议方法的类型。

  • Append​File  :  表示要用于将文件追加到 FTP 服务器上的现有文件的 FTP APPE 协议方法。
  • Delete​File    :表示要用于删除 FTP 服务器上的文件的 FTP DELE 协议方法。
  • Download​File    :表示要用于从 FTP 服务器下载文件的 FTP RETR 协议方法。
  • Get​Date​Timestamp    :表示要用于从 FTP 服务器上的文件检索日期时间戳的 FTP MDTM 协议方法。
  • Get​File​Size    :表示要用于检索 FTP 服务器上的文件大小的 FTP SIZE 协议方法。
  • List​Directory    :表示获取 FTP 服务器上的文件的简短列表的 FTP NLIST 协议方法。
  • List​Directory​Details    :表示获取 FTP 服务器上的文件的详细列表的 FTP LIST 协议方法。
  • Make​Directory    :表示在 FTP 服务器上创建目录的 FTP MKD 协议方法。
  • Print​Working​Directory    :表示打印当前工作目录的名称的 FTP PWD 协议方法。
  • Remove​Directory    :表示移除目录的 FTP RMD 协议方法。
  • Rename    :表示重命名目录的 FTP RENAME 协议方法。
  • Upload​File    :表示将文件上载到 FTP 服务器的 FTP STOR 协议方法。
  • Upload​File​With​Unique​Name    :表示将具有唯一名称的文件上载到 FTP 服务器的 FTP STOU 协议方法。

二、上传文件:

OpenFileDialog opFilDlg = new OpenFileDialog();
if (opFilDlg.ShowDialog() == DialogResult.OK)
{ ftp = new YBBFTPClass("hz.a.cn", "", "csp", "welcome", 0);
ftp.UploadFile(opFilDlg.FileName);
MessageBox.Show("上传成功");
}

/// <summary>
/// 文件上传
/// </summary>
/// <param name="filename">本地文件路径</param>
public void UploadFile(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + RemoteHost + "/" + fileInf.Name;
FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + RemoteHost + "/" + fileInf.Name));// 根据uri创建FtpWebRequest对象
reqFTP.Credentials = new NetworkCredential(RemoteUser, RemotePass); // ftp用户名和密码
reqFTP.KeepAlive = false; // 默认为true,连接不会被关闭, 在一个命令之后被执行
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定执行什么命令
reqFTP.UseBinary = true; // 指定数据传输类型
reqFTP.ContentLength = fileInf.Length; // 上传文件时通知服务器文件的大小 int contentLen;
FileStream fileStream = fileInf.OpenRead(); // 打开一个文件读取内容到fileStream中
contentLen = fileStream.Read(buffer, 0, buffer.Length); ;//从fileStream读取数据到buffer中 Stream requestStream = reqFTP.GetRequestStream();
// 流内容没有结束
while (contentLen != 0)
{
requestStream.Write(buffer, 0, contentLen);// 把内容从buffer 写入 requestStream中,完成上传。
contentLen = fileStream.Read(buffer, 0, buffer.Length);
} // 关闭两个流
requestStream.Close();
//uploadResponse = (FtpWebResponse)reqFTP.GetResponse();
fileStream.Close();
}

三、下载文件

1、核心代码

/// <summary>
/// 下载文件
/// </summary>
/// <param name="filePath">本地目录</param>
/// <param name="fileName">远程路径</param>
public void DownloadFile(string filePath, string fileName)
{
FtpWebRequest reqFTP;
try
{
FileStream fileStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + RemoteHost + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(RemoteUser, RemotePass); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();//从ftp响应中获得响应流 //long cl = response.ContentLength;
byte[] buffer = new byte[1024];
int readCount; readCount = responseStream.Read(buffer, 0, buffer.Length);//从ftp的responseStream读取数据到buffer中
while (readCount > 0)
{
fileStream.Write(buffer, 0, readCount);//从buffer读取数据到fileStream中,完成下载
readCount = responseStream.Read(buffer, 0, buffer.Length);
} responseStream.Close();
fileStream.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

2、winform:、

FolderBrowserDialog fldDlg = new FolderBrowserDialog();
if (txtUpload.Text.Trim().Length > 0)
{
if (fldDlg.ShowDialog() == DialogResult.OK)
{
ftp.DownloadFile(fldDlg.SelectedPath, txtUpload.Text.Trim());
MessageBox.Show("下载成功");
}
}
else
{
MessageBox.Show("Please enter the File name to download");
}

3、webform弹出下载提示:

FtpClient _client = new FtpClient();
Stream stream = _client.OpenRead(FtpFilePath, FtpDataType.Binary); string FtpFilePath = Request.QueryString["FilePath"];
string _fname = Path.GetFileName(FtpFilePath);
Response.ContentType = "application/" + _fname.Split('.')[1];
Response.AddHeader("Content-disposition", "attachment; filename=" + _fname); byte[] buffer = new byte[10240];
int readCount;
do
{
readCount = stream.Read(buffer, 0, buffer.Length);
Response.OutputStream.Write(buffer, 0, readCount);//持续写入流
} while (readCount != 0); Response.OutputStream.Write(buffer, 0, buffer.Length); Response.End();

四、删除文件

string uri = "ftp://" + RemoteHost + "/" + fileName;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + RemoteHost + "/" + fileName)); reqFTP.Credentials = new NetworkCredential(RemoteUser, RemotePass);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile; string result = String.Empty;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();

完整代码参考:

http://www.cnblogs.com/hsiang/p/7247801.html

FtpWebRequest与FtpWebResponse完成FTP操作的更多相关文章

  1. (转)FTP操作类,从FTP下载文件

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  2. C# FTP操作

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace FTP操作 { ...

  3. 关于FTP操作的功能类

    自己在用的FTP类,实现了检查FTP链接以及返回FTP没有反应的情况. public delegate void ShowError(string content, string title); // ...

  4. FtpHelper ftp操作类库

    FtpHelper ftp操作类库 using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  5. C# FTP操作类的代码

    如下代码是关于C# FTP操作类的代码.using System;using System.Collections.Generic;using System.Text;using System.Net ...

  6. 【转载】C#工具类:FTP操作辅助类FTPHelper

    FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而不需要进一步处理,就像MIME或Unicode一样.可以通过C#中的FtpWebRequest类.NetworkCredential类.We ...

  7. [转]C# FTP操作类

      转自 http://www.cnblogs.com/Liyuting/p/7084718.html using System; using System.Collections.Generic; ...

  8. 【C#】工具类-FTP操作封装类FTPHelper

    转载:http://blog.csdn.net/gdjlc/article/details/11968477 using System; using System.Collections.Generi ...

  9. PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )

    /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...

随机推荐

  1. git 找回 git reset --hard HEAD 后的代码

    下面方法只针对当你本地代码做了 git add 或则 git commit 后又手贱的重置本地代码到上一个版本,导致本地代码丢失的情况. 如果你没有 git add 命令,而直接 git reset ...

  2. Android sdk manager 下载速度慢的问题

    不多说了,直接附上方法: 首先打开Ecplise 中Android sdk manager,打开后, 在此窗口的上方打开偏好设置选项,然后在里面设置HTTP Proxy server和HTTP Pro ...

  3. 前端模块化(AMD和CMD、CommonJs)

    知识点1:AMD/CMD/CommonJs是JS模块化开发的标准,目前对应的实现是RequireJs/SeaJs/nodeJs. 知识点2:CommonJs主要针对服务端,AMD/CMD主要针对浏览器 ...

  4. Mybatis源码正确打开方式

    精心挑选要阅读的源码项目: 饮水思源——官方文档,先看文档再看源码: 下载源码,安装到本地,保证能编译运行: 从宏观到微观,从整体到细节: 找到入口,抓主放次,梳理核心流程: 源码调试,找到核心数据结 ...

  5. windows多线程窗口程序设计

    掌握windows基于消息驱动的窗口应用程序设计的基本方法,掌握窗口程序资源的概念与设计,掌握常用的消息的程序处理方法,掌握文字图形输出相关函数编程.掌握设计的基本方法(选项),掌握时钟消息设计动画程 ...

  6. Unity之使用技巧记录

    A:写了个死循环Unity无响应崩溃了怎么办? Q::到文件夹里找到刚刚写的脚本,把错误的代码屏蔽掉,再回到Unity

  7. (利用DOM)在新打开的页面点击关闭当前浏览器窗口

    1.在开发过程中我们前端的用户体验中有时候会要求点击一个按钮,关闭当前浏览器窗口.用html DOM就可做到. 2.注意:本次写法要求在新窗口中关闭. target="_blank" ...

  8. Springmvc中的HandlerAdaptor执行流程

    今天讲解一下在Springmvc中的HandlerAdaptor执行流程,明白这个过程,你就能画出下面的图: 接下来我们就来看看具体的实现过程吧. 1.0在DispatcherServlet中找到ge ...

  9. Linux常用指令大全

    2017-03-25   16:35:42 刚开始学习Linux,由于记忆力有限,把平时常用的Linux命令整理出来,以便随时查阅:  linux 基本命令    ls     (list 显示当前目 ...

  10. 微信小程序上传Excel文本文件功能

    问题: 在开发过程中会发现微信小程序有很多功能都还不能满足我们的需求,谁叫客户就是上帝呢,前几天小编遇到了这么个问题,就是用微信小程序上传文件,但是还以为微信带有这个模块,可是查了许久还是没有找到,只 ...