【问题标题】:Tracking upload progress of WebClient跟踪 WebClient 的上传进度
【发布时间】:2013-05-19 16:19:40
【问题描述】:

所以我目前正在使用以下代码通过我的网络服务器上的 php 脚本上传文件:

string file = "dp.jpg";

System.Net.WebClient Client = new System.Net.WebClient();
Client.Headers.Add("Content-Type", "binary/octet-stream");
byte[] result = Client.UploadFile("http://localhost/upload.php", "POST", file);

String response = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);

我想知道我将如何使用它,或者使用不同的方法来跟踪它已上传了多少并将其显示在进度条上?

谢谢。

【问题讨论】:

    标签: c# upload progress-bar webclient


    【解决方案1】:

    使用UploadFileAsync

    订阅wcUploader.UploadFileCompletedwcUploader.UploadProgressChangedevents 这样你就可以得到上传进度 和上传完成。

    在以下代码中,您可以检查我们如何订阅 UploadProgressChanged 和 我们可以得到e.ProgressPercentage 的值。

    检查以下sn-p:

    using System;
    using System.Net;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private readonly WebClient wcUploader = new WebClient();
    
            public Form1()
            {
                InitializeComponent();
    
                wcUploader.UploadFileCompleted += UploadFileCompletedCallback;
                wcUploader.UploadProgressChanged += UploadProgressCallback;
            }
    
    
            private void UploadFileCompletedCallback(object sender, UploadFileCompletedEventArgs e)
            {
                // a clever way to handle cross-thread calls and avoid the dreaded
                // "Cross-thread operation not valid: Control 'textBox1' accessed 
                // from a thread other than the thread it was created on." exception
    
                // this will always be called from another thread,
                // no need to check for InvokeRequired
                BeginInvoke(
                    new MethodInvoker(() =>
                        {
                            textBox1.Text = Encoding.UTF8.GetString(e.Result);
                            button1.Enabled = true;
                        }));
            }
    
            private void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
            {
                // a clever way to handle cross-thread calls and avoid the dreaded
                // "Cross-thread operation not valid: Control 'textBox1' accessed 
                // from a thread other than the thread it was created on." exception
    
                // this will always be called from another thread,
                // no need to check for InvokeRequired
    
                BeginInvoke(
                    new MethodInvoker(() =>
                        {
                            textBox1.Text = (string)e.UserState + "\n\n"
                                            + "Uploaded " + e.BytesSent + "/" + e.TotalBytesToSend
                                            + "b (" + e.ProgressPercentage + "%)";
                        }));
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = "";
    
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    button1.Enabled = false;
                    string toUpload = openFileDialog1.FileName;
                    textBox1.Text = "Initiating connection";
                    new Thread(() =>
                               wcUploader.UploadFileAsync(new Uri("http://anyhub.net/api/upload"), "POST", toUpload)).Start();
                }
            }
        }
    }
    

    从这里提取的代码sn-p:

    UploadFileAsync not asynchronous?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2017-09-12
      • 2023-03-31
      • 2016-06-02
      • 2016-03-09
      相关资源
      最近更新 更多