嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
csharp ftp开发中的客户端开发
private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" ftpServerIP "/" fileInf.Name;
FtpWebRequest reqFTP;
//通过前面得到的uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" ftpServerIP "/" fileInf.Name));
// 提供网络允许的基于密码的身份验证方案
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
//默认情况下KeepAlive是true
reqFTP.KeepAlive = false;
//指定要执行的命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
//指定数据的传输类型
reqFTP.UseBinary = true;
//指定上传文件的长度
reqFTP.ContentLength = fileInf.Length;
// 缓冲区大小设置成2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
//打开一个文件流来读入上传的文件
FileStream fs = fileInf.OpenRead();
try
{
// 把要上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
//从文件流中读取数据,一次读2kb大小的数据
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
//把文件的内容从文件流写到FTP上传流中
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
//关闭文件流和请求流
strm.Close();
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "上传出错");
}
}