【问题标题】:issue using progressbar to track ftp fiel upload使用进度条跟踪 ftp 文件上传的问题
【发布时间】:2015-10-08 17:02:45
【问题描述】:

所以我尝试使用进度条来直观地显示通过 ftp 上传文件的进度。 我似乎无法弄清楚为什么进度条和其他 3 个标签(labelSpeed、labelPerc、labelUploaded)不起作用。

任何帮助将不胜感激。谢谢。

PS:我附上了我的代码片段供查看。

Stopwatch sw = new Stopwatch(); //<-- The stopwatch which we will be using to calculate the upload speed

public void Upload(string ftpServer, string username, string password, string filename, string folder1, string folder2, string folder3)//<-- Uploads image to ftp directory
    {
        string ftpfolderpath = folder1 + "/" + folder2 + "/" + folder3;
        MakeFTPDir(ftpServer, ftpfolderpath, username, password); //<-- Makes the new directory, if there isnt one already

        using (WebClient client = new WebClient())
        {
            client.UploadFileCompleted += new UploadFileCompletedEventHandler(Completed); //<--NEW
            client.UploadProgressChanged += new UploadProgressChangedEventHandler(ProgressChanged); //<--NEW
            sw.Start(); //<-- Start the stopwatch which we will be using to calculate the upload speed

            client.Credentials = new NetworkCredential(username, password);
            client.UploadFile(ftpServer + "/" + folder1 + "/" + folder2 + "/" + folder3 + "/" + new FileInfo(filename).Name, "STOR", filename); //<-- uploads image to ftp Directory
        }
    }

    private void ProgressChanged(object sender, UploadProgressChangedEventArgs e) //<-- The event that will fire whenever the progress of the WebClient is changed
    {
        // Calculate upload speed and output it to labelSpeed.
        labelSpeed.Text = string.Format("{0} kb/s", (e.BytesSent / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));

        // Update the progressbar percentage only when the value is not the same.
        progressBar1.Value = e.ProgressPercentage;

        // Show the percentage on our label.
        labelPerc.Text = e.ProgressPercentage.ToString() + "%";

        // Update the label with how much data have been uploaded so far and the total size of the file we are currently uploading
        labelUploaded.Text = string.Format("{0} MB's / {1} MB's",
            (e.BytesSent / 1024d / 1024d).ToString("0.00"),
            (e.TotalBytesToSend / 1024d / 1024d).ToString("0.00"));
    }

    private void Completed(object sender, UploadFileCompletedEventArgs e) //<-- The event that will trigger when the WebClient is completed
    {
        if (e.Error != null)
        {
            string error = e.Error.ToString(); // error = "Custom error message here";
            MessageBox.Show(error);
            return;
        }

        sw.Reset(); // Reset the stopwatch.

        if (e.Cancelled == true)
        {
            MessageBox.Show("Image Upload has been canceled.");
        }
        else
        {
            MessageBox.Show("Image Upload completed!");
        }
    }

【问题讨论】:

标签: c# winforms ftp


【解决方案1】:

您正在使用 UploadFile 方法,它阻塞了调用 UI 线程。相反,在 GUI 线程上,应该使用UploadFileAsync 来不阻塞。

可以看到类似WebClient.UploadProgressChanged Event (System.Net)的例子

编辑::这就是我的意思。替换

client.UploadFile(ftpServer + "/" + folder1 + "/" + folder2 + "/" + folder3 + "/" + new FileInfo(filename).Name, "STOR", filename); //<-- uploads image to ftp Directory

client.UploadFileAsync(new Uri(ftpServer + "/" + folder1 + "/" + folder2 + "/" + folder3 + "/" + new FileInfo(filename).Name), "STOR", filename); //<-- uploads image to ftp Directory

【讨论】:

  • 我不明白的是,使用“UploadFileAsync”方法,我看不到您提供的示例链接中指定的上传位置(保存图像的位置)。所以我做了建议的改变: Uri uri = new Uri(picboxEmployeePic.ImageLocation); client.UploadFileAsync(uri, "POST", 文件名);现在我得到一个错误。以下图片供参考:linklink
  • @saturobi360 我想你误解了我的意思。我的意思是在UploadFile 的末尾添加Async(如client.UploadFileAsync(ftpServer + "/" + folder1 + "/" ...
  • 当我将“异步”添加到“上传文件”的末尾时,我得到了 System.ArgumentNullException 和 System.Net.WebException。该错误还指出 'System.Net.WebClient.UploadFileAsync(System.Uri, string, string) 的重载方法匹配具有一些无效参数。下面是截图Link
  • 非常感谢。标签现在可以工作了。现在只是想知道为什么进度条不起作用。我需要为进度条分配一组属性才能使其工作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2014-11-15
  • 1970-01-01
  • 2012-01-01
  • 2012-08-18
  • 1970-01-01
相关资源
最近更新 更多