FtpWebRequest FTP download with ProgressBar(FtpWebRequest 使用 ProgressBar 进行 FTP 下载)
问题描述
我的代码可以运行,但是 ProgressBar
直接跳转到 100% 并且继续下载.完成后会出现一个消息框来获取信息.
My code works, but the ProgressBar
jumps directly to 100% and the download will go on. When its finished then comes a messageBox to take a Info.
我已经更改了缓冲区大小,但没关系.
I have already changed the buffer size, but it doesn't matter.
我在这里做错了什么?
这是我的代码:
void workerDOWN_DoWork(object sender, DoWorkEventArgs e)
{
string fileFullPath = e.Argument as String;
string fileName = Path.GetFileName(fileFullPath);
string fileExtension = Path.GetExtension(fileName);
label4.Invoke((MethodInvoker)delegate { label4.Text = "Downloading File.."; });
string ftpServerIP = "XXX";
string ftpUserName = "XXX";
string ftpPassword = "XXX";
try
{
//Datei vom FTP Server downloaden
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpServerIP + "/" + fileName);
request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(fileFullPath))
{
var buffer = new byte[32 * 1024];
int totalReadBytesCount = 0;
int readBytesCount;
while ((readBytesCount = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, readBytesCount);
totalReadBytesCount += readBytesCount;
var progress = (int)((float)totalReadBytesCount / (float)fileStream.Length * 100);
workerDOWN.ReportProgress((int)progress);
label3.Invoke((MethodInvoker)delegate { label3.Text = progress + " %"; });
}
}
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
MessageBox.Show("Datei nicht gefunden!", "Error");
}
}
e.Result = fileFullPath;
}
void workerDOWN_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
string fileFullPath = e.Result as String;
string fileName = Path.GetFileName(fileFullPath);
MessageBox.Show("Download erfolgreich!","Information");
progressBar1.Value = 0;
label3.Invoke((MethodInvoker)delegate { label3.Text = " "; });
label4.Invoke((MethodInvoker)delegate { label4.Text = " "; });
}
推荐答案
使用 FtpWebRequest
与 WinForms 进度条进行 FTP 下载的简单示例:
Trivial example of FTP download using FtpWebRequest
with WinForms progress bar:
private void button1_Click(object sender, EventArgs e)
{
// Run Download on background thread
Task.Run(() => Download());
}
private void Download()
{
try
{
const string url = "ftp://ftp.example.com/remote/path/file.zip";
NetworkCredential credentials = new NetworkCredential("username", "password");
// Query size of the file to be downloaded
WebRequest sizeRequest = WebRequest.Create(url);
sizeRequest.Credentials = credentials;
sizeRequest.Method = WebRequestMethods.Ftp.GetFileSize;
int size = (int)sizeRequest.GetResponse().ContentLength;
progressBar1.Invoke(
(MethodInvoker)(() => progressBar1.Maximum = size));
// Download the file
WebRequest request = WebRequest.Create(url);
request.Credentials = credentials;
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:localpathfile.zip"))
{
byte[] buffer = new byte[10240];
int read;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
int position = (int)fileStream.Position;
progressBar1.Invoke(
(MethodInvoker)(() => progressBar1.Value = position));
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
核心下载代码基于:
在 C#/.NET 中向/从 FTP 服务器上传和下载二进制文件
解释为什么你的代码不起作用:
To explain why your code does not work:
- 您正在使用目标文件的大小进行计算:
fileStream.Length
– 它始终等于totalReadBytesCount
,因此是progress
永远是 100. - 您可能打算使用
ftpStream.Length
,但无法读取. - 基本上使用 FTP 协议,您不知道正在下载的文件的大小.如果你需要知道它,你必须在下载之前明确地查询它.在这里,我使用
WebRequestMethods.Ftp.GetFileSize
.
- You are using size of the target file for the calculation:
fileStream.Length
– It will always be equal tototalReadBytesCount
, hence theprogress
will always be 100. - You probably meant to use
ftpStream.Length
, but that cannot be read. - Basically with FTP protocol, you do not know size of the file you are downloading. If you need to know it, you have to query it explicitly before the download. Here I use the
WebRequestMethods.Ftp.GetFileSize
for that.
这篇关于FtpWebRequest 使用 ProgressBar 进行 FTP 下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FtpWebRequest 使用 ProgressBar 进行 FTP 下载


- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01