System.Net命名空间下的FtpWebRequest类实现了ftp协议的.Net实现。

  • FtpWebRequest.KeepAlive指定在请求完成后服务器是否要马上关闭连接
  • FtpWebRequest.UseBinary 指定文件以二进制方式传输
  • FtpWebRequest.Method设置ftp的命令
  • WebRequestMethods.Ftp.UploadFile是上传文件的命令

使用FtpWebRequest对象上传文件时,需要向GetRequestStream方法返回的Stream中写入数据。

需要引用如下命名空间

using System.Net;
using System.IO;

FTP上传文件代码实现:

public void ftpfile(string ftpfilepath, string inputfilepath)
{
string ftphost = "127.0.0.1";
//here correct hostname or IP of the ftp server to be given string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential("userid", "password");
//userid and password for the ftp server to given ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(inputfilepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}

调用方法:

ftpfile(@"/testfolder/testfile.xml", @"c:\testfile.xml");

C#使用FtpWebRequest上传文件的更多相关文章

  1. ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合

    一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...

  2. FTP上传文件到服务器

    一.初始化上传控件. 1.我们这里用dropzone.js作为上传控件,下载地址http://www.dropzonejs.com/ 2.这里我们使用一个div元素作为dropzone载体. < ...

  3. 再看ftp上传文件

    前言 去年在项目中用到ftp上传文件,用FtpWebRequest和FtpWebResponse封装一个帮助类,这个在网上能找到很多,前台使用Uploadify控件,然后在服务器上搭建Ftp服务器,在 ...

  4. ASP.NET跨服务器上传文件的相关解决方案

    第一种:通过FTP来上传文件 首先,在另外一台服务器上设置好FTP服务,并创建好允许上传的用户和密码,然后,在ASP.NET里就可以直接将文件上传到这台 FTP 服务器上了.代码如下: <%@ ...

  5. .net ftp上传文件方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  6. FTP上传文件夹

    文件上传类 using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; usi ...

  7. FTP 上传文件

    有时候需要通过FTP同步数据文件,除了比较稳定的IDE之外,我们程序员还可以根据实际的业务需求来开发具体的工具,具体的开发过程就不细说了,这里了解一下通过C#实现FTP上传文件到指定的地址. /// ...

  8. C# FTP上传文件至服务器代码

    C# FTP上传文件至服务器代码 /// <summary> /// 上传文件 /// </summary> /// <param name="fileinfo ...

  9. asp.net 服务器 上传文件到 FTP服务器

    private string ftpServerIP = "服务器ip";//服务器ip private string ftpUserID = "ftp的用户名" ...

随机推荐

  1. GCJ 2015-Qualification-C Dijkstra 特殊注意,展开 难度:2

    https://code.google.com/codejam/contest/6224486/dashboard#s=p2 题目中的新运算满足传递性不满足自反性,满足传递性则可以先计算后面的部分再计 ...

  2. phpstrom+xdebug+Xdebug helper 调试php

    第一步,php.ini打开xdebug扩展 xdebug.remote_enable=on ; 此地址为IDE所在IP xdebug.remote_host=127.0.0.1 xdebug.remo ...

  3. javascript photo1

  4. format 字符串

    http://www.cnblogs.com/mumble/archive/2011/05/25/2056462.html ShowMessage( Format('this is %.0f',[ t ...

  5. 计算文字的高度和宽度--以微博会话界面中用户名(userName)为例

    所用方法 // NOTE: All of the following methods will default to drawing on a baseline, limiting drawing t ...

  6. YAML 语言语法

    发现很多开源的软件的配置文件都使用了这种语言来描述,据说是简单强大,很不巧,ansible也使用了这种语言来描述配置,学习ansible之前,先学习一下YAML语言. YAML基本语法规则如下: 大小 ...

  7. exec方法

    如果 exec 方法没有找到匹配,将返回 null.如果找到匹配项,则 exec 方法返回一个数组,并将更新全局 RegExp 对象的属性以反映匹配结果.数组元素 0 包含了完整的匹配项,而元素 1 ...

  8. poj1845 数论

    //Accepted 204K 16MS //约数和 //n=p1^e1*p2^e2***pk^ek //约数和为:(p1^0+p1^1+..+p1^e1)*(p2^0+p2^1+..+p2^e2)* ...

  9. json_decode 与 json_encode 的区别

    1.json_decode对JSON格式的字符串进行编码 2.json_encode对变量进行 JSON 编码 3.unset()是注销定义的变量 4.urlencode()函数原理就是首先把中文字符 ...

  10. keil(持续更新)

    1函数格式提示 2  cording时有警告和错误提示 3 类的成员 提示