您的位置:首页 > 编程语言 > C#

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: