c#Ftp上传文件


public string UploadFile(string filePath, string fileName, string ftpServerIP, string ftpUserName, string ftpPassword)
{
string file = filePath + fileName;
FileInfo fileInf = new FileInfo(file);

string uri = "ftp://" + ftpServerIP + @"/" + fileInf.Name;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

reqFtp.Method = WebRequestMethods.Ftp.UploadFile;

reqFtp.UsePassive = false;
reqFtp.UseBinary = true;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen = 0;
FileStream fs = fileInf.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();
}
catch (Exception ex)
{
fs.Close();
return ex.StackTrace;
}
return "文件上传成功";
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM