C#上传文件到ftp
2012-08-08 11:22
239 查看
private void UploadImg(string sFileDstPath, string FolderName, string ftpServerIP, string ftpUserName,string ftpPwd)
{
FileInfo fileInf = new FileInfo(sFileDstPath);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest) FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + FolderName + "/" +fileInf.Name));
reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
using (FileStream fs = fileInf.OpenRead())
{
using (Stream strm = reqFTP.GetRequestStream())
{
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
}
fs.Close();
}
}
{
FileInfo fileInf = new FileInfo(sFileDstPath);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest) FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + FolderName + "/" +fileInf.Name));
reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
using (FileStream fs = fileInf.OpenRead())
{
using (Stream strm = reqFTP.GetRequestStream())
{
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
}
fs.Close();
}
}
相关文章推荐
- C#上传下载文件ftp操作类FTPClient代码
- c# FTP上传文件。
- net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- c#ftp操作全解:创建删除目录,上传下载文件,删除移动文件,文件改名,文件目录查询
- C#上传下载文件ftp操作类FTPClient代码
- C#对.CSV格式的文件--逗号分隔值文件 的读写操作及上传ftp服务器操作方法总结
- C#FTP方式文件上传远程服务器
- C# FTP上传文件至服务器代码
- C# WebClient进行FTP服务上传文件和下载文件
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- 使用C#实现FTP的文件上传和下载【转】
- 【转】C# 上传本地文件至ftp上的方法
- c/s模式 (C#)下Ftp的多文件上传及其上传进度
- c# FTP上传文件实例代码(简易版)
- C# 之 FTP服务器中文件上传与下载(四)
- 使用ftp上传文件和下载文件---C#
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- 【转载】C#如何操控FTP,获取FTP文件或文件夹列表,获取FTP文件大小,FTP上传,FTP删除文件,FTP新建文件夹、删除文件夹