#region 上传文件
/// <summary>
/// 上传文件
/// </summary>
/// <param name="filename"></param>
public bool Upload(string filename, string path, out string errorinfo) //上面的代码实现了从ftp服务器上载文件的功能
{
path = path.Replace("\\", "/");
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + path + "/" + fileInf.Name;
Connect(uri);//连接
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
req FTP.KeepAlive = false;
// 指定执行什么命令
req FTP.Method = WebRequestMethods. Ftp.UploadFile;
// 上传文件时通知服务器文件的大小
req FTP.ContentLength = fileInf.Length;
// 缓冲大小设置为kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流(System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
// 把上传的文件写入流
Stream strm = req FTP.GetRequestStream();
// 每次读文件流的kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
errorinfo = "完成";
return true;
}
catch (Exception ex)
{
errorinfo = string.Format("因{0},无法完成上传", ex.Message);
return false;
}
}
#endregion
#region 续传文件
param name/// <summary>
/// 续传文件
/// </summary>
/// <param name="filename"></param>
public bool Upload(string filename, long size,string path, out string errorinfo) //上面的代码实现了从ftp服务器上载文件的功能
{
path = path.Replace("\\", "/");
FileInfo fileInf = new FileInfo(filename);
//string uri = "ftp://" + path + "/" + fileInf.Name;
string uri = "ftp://" + path;
Connect(uri);//连接
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
req FTP.KeepAlive = false;
// 指定执行什么命令
req FTP.Method = WebRequestMethods. Ftp.AppendFile;
// 上传文件时通知服务器文件的大小
req FTP.ContentLength = fileInf.Length;
// 缓冲大小设置为kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流(System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
StreamReader dsad = new StreamReader(fs);
fs.Seek(size, SeekOrigin.Begin);
// 把上传的文件写入流
Stream strm = req FTP.GetRequestStream();
// 每次读文件流的kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
errorinfo = "完成";
return true;
}
catch (Exception ex)
{
errorinfo = string.Format("因{0},无法完成上传", ex.Message);
return false;
}
}
#endregion
#region 下载文件
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileName"></param>
/// <param name="errorinfo"></param>
/// <returns></returns>
public bool Download(string ftpfilepath, string filePath, string fileName, out string errorinfo)////上面的代码实现了从ftp服务器下载文件的功能
{
try
{
filePath = filePath.Replace("我的电脑\\", "");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论