String url = " ftp://192.168.1.1/测试.zip";
String localFileName = "ceshi.zip";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = null;
StreamReader readStream = null;
StreamWriter writeStream = null;
try
{
responseStream = response.GetResponseStream();
readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);
// Display information about the data received from the server.
Console.WriteLine("Bytes received: {0}", response.ContentLength);
Console.WriteLine("Message from server: {0}", response.StatusDescription);
Console.WriteLine("Resource: {0}", response.ResponseUri);
// Write the bytes received from the server to the local file.
if (readStream != null)
{
writeStream = new StreamWriter(localFileName, false);
writeStream.Write(readStream.ReadToEnd());
}
}
代码如上 ,然而当我通过 response.ContentLength 获得下载文件大小的时候,得到的值总是为 -1,
我是想改动上面的代码在windows应用程序中使用,同时添加一个进度条
19 个解决方案
#1
FTP是不发送ContentLength的……
#2
http://topic.csdn.net/u/20070820/10/a376fb03-220d-4e70-ada8-1a233e1895cc.html
参考这个帖子
参考这个帖子
#3
帖子被删,再发贴问为什么删有被删,没办法只好来此跟帖问问楼主为什么删我帖子了
#4
int fileSize1 = Request.Files[ "mainResourceFile "].ContentLength;
二楼说这个可行么?
我先去试试 谢谢关注
二楼说这个可行么?
我先去试试 谢谢关注
#5
http://down.ddvip.com/view/118820680312665.html
http://www.cnblogs.com/analyzer/articles/1218923.html
http://www.cnblogs.com/analyzer/articles/1218923.html
#6
mark
#7
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循环过程中你可以更新滚动条
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循环过程中你可以更新滚动条
#8
哦,不才,搞错了,你是下载操作
#9
帮你顶
#10
把readStream.ReadToEnd());改成按字节数读取应该可以
#11
up
#12
不就说明Ftp不发送ContentLength的嘛,换种思路吧
#13
mark
#14
谢谢各位关注
contentlength 不可行 大家还有其他法子么
下载文件的时候 加个进度条,windows 从ftp下载文件的时候是带有进度条的,该怎么做呢》?
contentlength 不可行 大家还有其他法子么
下载文件的时候 加个进度条,windows 从ftp下载文件的时候是带有进度条的,该怎么做呢》?
#15
晕倒,搂主还没解决掉?
你可以先用下面.NET提供的方法:
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
或者调用Windows API
WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szResPath, stFileInfo, 0, 0);
得到文件的大小
然后继续你得方法循环读取,但是是每次指定的字节数,而不是用ReadToEnd方法
应该可以吧
你可以先用下面.NET提供的方法:
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
或者调用Windows API
WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szResPath, stFileInfo, 0, 0);
得到文件的大小
然后继续你得方法循环读取,但是是每次指定的字节数,而不是用ReadToEnd方法
应该可以吧
#16
晕倒,搂主还没解决掉?
你可以先用下面.NET提供的方法:
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
或者调用Windows API
WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szResPath, stFileInfo, 0, 0);
得到文件的大小
然后继续你得方法循环读取,但是是每次读取指定的字节数,而不是用ReadToEnd方法
应该可以吧
你可以先用下面.NET提供的方法:
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
或者调用Windows API
WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szResPath, stFileInfo, 0, 0);
得到文件的大小
然后继续你得方法循环读取,但是是每次读取指定的字节数,而不是用ReadToEnd方法
应该可以吧
#17
FtpWebRequest ftpRequest = FtpWebRequest.Create(new Uri("ftp://....")) as FtpWebRequest;
ftpRequest.UseBinary = true;
ftpRequest.Timeout = 120000;
ftpRequest.Credentials = new NetworkCredential("emr", "emr");
ftpRequest.KeepAlive = false;
long fileSize = 0;
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = ftpRequest.GetResponse() as FtpWebResponse;
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength;
ftpStream.Close();
response.Close();
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
response = ftpRequest.GetResponse() as FtpWebResponse;
Stream downloadStream = response.GetResponseStream();
int bufferLength = 1024;
byte[] buffer = new byte[bufferLength];
int count = (int)(fileSize / (long)bufferLength);
if (fileSize % bufferLength != 0)
count++;
//
//设置滚动条值为count
FileInfo fileInfo = new FileInfo("c:\\a.tmp");
FileStream localFileStream = fileInfo.OpenWrite();
int contentLen = downloadStream.Read(buffer, 0, bufferLength);
while (contentLen != 0)
{
localFileStream.Write(buffer, 0, contentLen);
contentLen = downloadStream.Read(buffer, 0, bufferLength);
//
//滚动条值加1
}
downloadStream.Close();
localFileStream.Close();
response.Close();
匆匆搞了下,不知对否
ftpRequest.UseBinary = true;
ftpRequest.Timeout = 120000;
ftpRequest.Credentials = new NetworkCredential("emr", "emr");
ftpRequest.KeepAlive = false;
long fileSize = 0;
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = ftpRequest.GetResponse() as FtpWebResponse;
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength;
ftpStream.Close();
response.Close();
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
response = ftpRequest.GetResponse() as FtpWebResponse;
Stream downloadStream = response.GetResponseStream();
int bufferLength = 1024;
byte[] buffer = new byte[bufferLength];
int count = (int)(fileSize / (long)bufferLength);
if (fileSize % bufferLength != 0)
count++;
//
//设置滚动条值为count
FileInfo fileInfo = new FileInfo("c:\\a.tmp");
FileStream localFileStream = fileInfo.OpenWrite();
int contentLen = downloadStream.Read(buffer, 0, bufferLength);
while (contentLen != 0)
{
localFileStream.Write(buffer, 0, contentLen);
contentLen = downloadStream.Read(buffer, 0, bufferLength);
//
//滚动条值加1
}
downloadStream.Close();
localFileStream.Close();
response.Close();
匆匆搞了下,不知对否
#18
谢关注
试下去
试下去
#19
关注中。
项贴有分
项贴有分
#20
#1
FTP是不发送ContentLength的……
#2
http://topic.csdn.net/u/20070820/10/a376fb03-220d-4e70-ada8-1a233e1895cc.html
参考这个帖子
参考这个帖子
#3
帖子被删,再发贴问为什么删有被删,没办法只好来此跟帖问问楼主为什么删我帖子了
#4
int fileSize1 = Request.Files[ "mainResourceFile "].ContentLength;
二楼说这个可行么?
我先去试试 谢谢关注
二楼说这个可行么?
我先去试试 谢谢关注
#5
http://down.ddvip.com/view/118820680312665.html
http://www.cnblogs.com/analyzer/articles/1218923.html
http://www.cnblogs.com/analyzer/articles/1218923.html
#6
mark
#7
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循环过程中你可以更新滚动条
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循环过程中你可以更新滚动条
#8
哦,不才,搞错了,你是下载操作
#9
帮你顶
#10
把readStream.ReadToEnd());改成按字节数读取应该可以
#11
up
#12
不就说明Ftp不发送ContentLength的嘛,换种思路吧
#13
mark
#14
谢谢各位关注
contentlength 不可行 大家还有其他法子么
下载文件的时候 加个进度条,windows 从ftp下载文件的时候是带有进度条的,该怎么做呢》?
contentlength 不可行 大家还有其他法子么
下载文件的时候 加个进度条,windows 从ftp下载文件的时候是带有进度条的,该怎么做呢》?
#15
晕倒,搂主还没解决掉?
你可以先用下面.NET提供的方法:
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
或者调用Windows API
WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szResPath, stFileInfo, 0, 0);
得到文件的大小
然后继续你得方法循环读取,但是是每次指定的字节数,而不是用ReadToEnd方法
应该可以吧
你可以先用下面.NET提供的方法:
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
或者调用Windows API
WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szResPath, stFileInfo, 0, 0);
得到文件的大小
然后继续你得方法循环读取,但是是每次指定的字节数,而不是用ReadToEnd方法
应该可以吧
#16
晕倒,搂主还没解决掉?
你可以先用下面.NET提供的方法:
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
或者调用Windows API
WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szResPath, stFileInfo, 0, 0);
得到文件的大小
然后继续你得方法循环读取,但是是每次读取指定的字节数,而不是用ReadToEnd方法
应该可以吧
你可以先用下面.NET提供的方法:
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
或者调用Windows API
WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szResPath, stFileInfo, 0, 0);
得到文件的大小
然后继续你得方法循环读取,但是是每次读取指定的字节数,而不是用ReadToEnd方法
应该可以吧
#17
FtpWebRequest ftpRequest = FtpWebRequest.Create(new Uri("ftp://....")) as FtpWebRequest;
ftpRequest.UseBinary = true;
ftpRequest.Timeout = 120000;
ftpRequest.Credentials = new NetworkCredential("emr", "emr");
ftpRequest.KeepAlive = false;
long fileSize = 0;
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = ftpRequest.GetResponse() as FtpWebResponse;
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength;
ftpStream.Close();
response.Close();
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
response = ftpRequest.GetResponse() as FtpWebResponse;
Stream downloadStream = response.GetResponseStream();
int bufferLength = 1024;
byte[] buffer = new byte[bufferLength];
int count = (int)(fileSize / (long)bufferLength);
if (fileSize % bufferLength != 0)
count++;
//
//设置滚动条值为count
FileInfo fileInfo = new FileInfo("c:\\a.tmp");
FileStream localFileStream = fileInfo.OpenWrite();
int contentLen = downloadStream.Read(buffer, 0, bufferLength);
while (contentLen != 0)
{
localFileStream.Write(buffer, 0, contentLen);
contentLen = downloadStream.Read(buffer, 0, bufferLength);
//
//滚动条值加1
}
downloadStream.Close();
localFileStream.Close();
response.Close();
匆匆搞了下,不知对否
ftpRequest.UseBinary = true;
ftpRequest.Timeout = 120000;
ftpRequest.Credentials = new NetworkCredential("emr", "emr");
ftpRequest.KeepAlive = false;
long fileSize = 0;
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = ftpRequest.GetResponse() as FtpWebResponse;
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength;
ftpStream.Close();
response.Close();
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
response = ftpRequest.GetResponse() as FtpWebResponse;
Stream downloadStream = response.GetResponseStream();
int bufferLength = 1024;
byte[] buffer = new byte[bufferLength];
int count = (int)(fileSize / (long)bufferLength);
if (fileSize % bufferLength != 0)
count++;
//
//设置滚动条值为count
FileInfo fileInfo = new FileInfo("c:\\a.tmp");
FileStream localFileStream = fileInfo.OpenWrite();
int contentLen = downloadStream.Read(buffer, 0, bufferLength);
while (contentLen != 0)
{
localFileStream.Write(buffer, 0, contentLen);
contentLen = downloadStream.Read(buffer, 0, bufferLength);
//
//滚动条值加1
}
downloadStream.Close();
localFileStream.Close();
response.Close();
匆匆搞了下,不知对否
#18
谢关注
试下去
试下去
#19
关注中。
项贴有分
项贴有分