日期:2014-05-18  浏览次数:20919 次

从ftp下载文件 用进度条显示进度-求教
C# code
   String url = " ftp://192.168.1.1/测试.zip";
            String localFileName =  


代码如上 ,然而当我通过 response.ContentLength 获得下载文件大小的时候,得到的值总是为 -1,
我是想改动上面的代码在windows应用程序中使用,同时添加一个进度条 


------解决方案--------------------
FTP是不发送ContentLength的……
------解决方案--------------------
http://topic.csdn.net/u/20070820/10/a376fb03-220d-4e70-ada8-1a233e1895cc.html

参考这个帖子
------解决方案--------------------
帖子被删,再发贴问为什么删有被删,没办法只好来此跟帖问问楼主为什么删我帖子了
------解决方案--------------------
http://down.ddvip.com/view/118820680312665.html
http://www.cnblogs.com/analyzer/articles/1218923.html
------解决方案--------------------
FileInfo fileInfo = new FileInfo(szLocalFile);
if (fileInfo == null || !fileInfo.Exists)
return GlobalData.ReturnValue.FAILED;

string szBasePath = string.Format("ftp://{0}:{1}/", this.m_szIP, this.m_nPort.ToString());
szRemoteFile = szBasePath + szRemoteFile;

// 根据uri创建FtpWebRequest对象
FtpWebRequest ftpRequest = FtpWebRequest.Create(new Uri(szRemoteFile)) as FtpWebRequest;

// 指定数据传输类型
ftpRequest.UseBinary = true;

// 设置超时时间
ftpRequest.Timeout = 120000;

// ftp用户名和密码
ftpRequest.Credentials = new NetworkCredential(this.m_szUserName, this.m_szPassword);

// 默认为true,连接不会被关闭,在一个命令之后被执行
ftpRequest.KeepAlive = false;

// 指定执行什么命令
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

// 上传文件时通知服务器文件的大小
ftpRequest.ContentLength = fileInfo.Length;

// 缓冲大小设置为2KB 
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];

int contentLen = 0;

FileStream localFileStream = fileInfo.OpenRead();
try
{
// 把上传的文件写入流
Stream uploadStream = ftpRequest.GetRequestStream();

// 每次读文件流的kb
contentLen = localFileStream.Read(buffer, 0, bufferLength);

// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入upload stream 
uploadStream.Write(buffer, 0, contentLen);
contentLen = localFileStream.Read(buffer, 0, bufferLength);
}
// 关闭两个流
uploadStream.Close();
localFileStream.Close();
return GlobalData.ReturnValue.OK;
}
catch (Exception ex)
{
return GlobalData.ReturnValue.FAILED;
}


上面的while循环过程中你可以更新滚动条
------解决方案--------------------
哦,不才,搞错了,你是下载操作
------解决方案--------------------
帮你顶
------解决方案--------------------
把readStream.ReadToEnd());改成按字节数读取应该可以
------解决方案--------------------
引用楼主 xl_0715 的帖子:
C# code String url = " ftp://192.168.1.1/测试.zip";