FtpWebRequest





5.00/5 (1投票)
FtpWebRequest 类用于将文件上传到 FTP 服务器(目标位置)。此函数演示如何将文件上传到 FTP 服务器。此函数仅返回
FtpWebRequest 类用于将文件上传到 FTP 服务器(目标位置)。
此函数演示如何将文件上传到 FTP 服务器。此函数仅返回 true 或 false
如果上传成功,则返回 true,否则返回 false
public bool Upload(FileInfo fileInfo)
{
string uri = "";
string serverIp = "Studentacad.com";
string Username = "Aamir";
string Password = "Hasan";
uri = "ftp://" + serverIp + "/" + fileInfo.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(Username, Password);
reqFTP.KeepAlive = false;
reqFTP.Proxy = null;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.Timeout = 8000;
reqFTP.ContentLength = fileInfo.Length;
int buffLength = 433664;//1048;// *16;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInfo.OpenRead();
try
{
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();
return true;
}
catch (Exception ex)
{
if (reqFTP != null)
{
reqFTP.Abort();
}
return false
}
}
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx