问题描述
所以我目前正在使用以下代码通过我的网络服务器上的 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.UploadFileCompleted
和wcUploader.UploadProgressChanged
事件,以便您可以获取上传进度以及上传完成.
Suscribe to wcUploader.UploadFileCompleted
and wcUploader.UploadProgressChanged
events so you can get the upload progressand the upload completion.
在下面的代码中,您可以检查我们如何订阅 UploadProgressChanged 和我们可以得到 e.ProgressPercentage
值.
In the following code you can check how we suscribe to UploadProgressChanged andwe 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:
这篇关于跟踪WebClient的上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!