跟踪WebClient的上传进度 [英] Tracking upload progress of WebClient

查看:59
本文介绍了跟踪WebClient的上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am currently uploading files via a php script on my webserver using the following code:

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);

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

What I want to know is how would I use this, or a different method to track how much it has uploaded and display it on a progress bar?

谢谢.

推荐答案

使用 UploadFileAsync.

订阅 wcUploader.UploadFileCompletedwcUploader.UploadProgressChanged 事件,以便您可以获取上传进度以及上传完成.

Suscribe to wcUploader.UploadFileCompleted and wcUploader.UploadProgressChangedevents so you can get the upload progress and the upload completion.

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

In the following code you can check how we suscribe to UploadProgressChanged and we can get e.ProgressPercentage value.

检查以下代码段:

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();
            }
        }
    }
}

从这里提取的代码片段:

Code snippet extracted from here:

UploadFileAsync 不是异步的?

这篇关于跟踪WebClient的上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆