2013-05-24 4 views
0

그래서 현재 다음과 같은 코드를 사용하여 내 웹 서버에 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); 

는 내가 알고 싶은 것은, 또는 다른 방법을 업로드 한 파일의 양을 추적하고 진행률 막대에 표시합니다.

감사합니다.

답변

1

UploadFileAsync을 사용하십시오. wcUploader.UploadFileCompletedwcUploader.UploadProgressChanged 이벤트에

Suscribe 당신이 업로드 진행 및 업로드 완료를 얻을 수 있습니다.

다음 코드에서 우리는 UploadProgressChanged 및 에 대한 응답을 확인하여 e.ProgressPercentage 값을 얻을 수 있습니다.

확인 다음 코드 :

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

코드는 여기에서 추출 니펫

UploadFileAsync not asynchronous?

관련 문제