/// <summary>
/// 上传文件
/// </summary>
/// <param name="ftpUrl">ftp URL</param>
/// <param name="fielName">文件名称</param>
public static void FTPUpload(string ftpUrl, string fielName)
{
FileInfo Info = new FileInfo(fielName);
string FielEasyName = Info.Name;
Uri uri = new Uri(ftpUrl + FielEasyName);
FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(uri);
//身份验证
ftpReq.Credentials = new NetworkCredential(username, userpwd);
//设置文件大小
ftpReq.ContentLength = Info.Length;
ftpReq.Method = System.Net.WebRequestMethods.Ftp.UploadFile;//设置上传请求
//指定文件传输的数据类型
ftpReq.UseBinary = true;
ftpReq.UsePassive = true;
// ftpReq.KeepAlive = true;
//获取请求流
Stream reqStream = ftpReq.GetRequestStream();
//设置缓冲池
byte[] arr = null;
using (FileStream fs = new FileStream(fielName, FileMode.Open))
{
int PCount = 1;//设置指针开始
while (PCount > 0)
{
arr = new byte[1024];
PCount = fs.Read(arr, 0, arr.Length);
reqStream.Write(arr, 0, arr.Length);
}
reqStream.Close();
fs.Close();
}
}
/// 上传文件
/// </summary>
/// <param name="ftpUrl">ftp URL</param>
/// <param name="fielName">文件名称</param>
public static void FTPUpload(string ftpUrl, string fielName)
{
FileInfo Info = new FileInfo(fielName);
string FielEasyName = Info.Name;
Uri uri = new Uri(ftpUrl + FielEasyName);
FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(uri);
//身份验证
ftpReq.Credentials = new NetworkCredential(username, userpwd);
//设置文件大小
ftpReq.ContentLength = Info.Length;
ftpReq.Method = System.Net.WebRequestMethods.Ftp.UploadFile;//设置上传请求
//指定文件传输的数据类型
ftpReq.UseBinary = true;
ftpReq.UsePassive = true;
// ftpReq.KeepAlive = true;
//获取请求流
Stream reqStream = ftpReq.GetRequestStream();
//设置缓冲池
byte[] arr = null;
using (FileStream fs = new FileStream(fielName, FileMode.Open))
{
int PCount = 1;//设置指针开始
while (PCount > 0)
{
arr = new byte[1024];
PCount = fs.Read(arr, 0, arr.Length);
reqStream.Write(arr, 0, arr.Length);
}
reqStream.Close();
fs.Close();
}
}