2010-12-02 53 views
0

我有一個格式爲http://ww.url.com/page?id=200的網址。這將下載一個10 MB的文件。不過,我總是將e.ProgressPercentage設爲0。但它實際上讀取的結果是上傳完成函數。下載AsyncData的WebClient失敗

請告訴我我在做什麼錯在這裏。

我使用以下代碼

private void button1_Click(object 
    sender, EventArgs e) 
{ 

    WebClient webClient = new WebClientEx(); 

    webClient.DownloadProgressChanged += 
     new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); 

    webClient.DownloadDataCompleted += new 
     DownloadDataCompletedEventHandler(wc_DownloadComplete); 

    webClient.DownloadDataAsync(new Uri("http://ww.url.com/page?id=200")); 


} 

public void wc_DownloadProgressChanged(Object sender, 
    DownloadProgressChangedEventArgs e) 

{ 
    Progress1.Value = e.ProgressPercentage; // this is 0 
} 

public void wc_DownloadComplete(Object sender,DownloadDataCompletedEventArgs e) 

{ 
    int a = 0; 
    this.Close(); 
} 
} } 
+0

也許它是由您的格式造成的( - :) – 2010-12-02 21:08:30

回答

0

ProgressPercentage是基於所述的ContentLength在響應的報頭中返回。如果響應不知道在將標題放在一起時的下載量有多大,則無法取得進展。

如果這只是IIS返回的文件,IIS將從文件系統獲取文件的大小併爲您設置標題。如果響應是動態的並且適合緩衝區,則可以設置響應的長度。但是,我相當肯定默認緩衝區大小小於10MB,因此在響應的長度已知之前將響應頭髮送回客戶端。

在Asp.Net,你可以這樣做(如果你把它取決於您的平臺)...

Response.AddHeader("Content-Length", "10485760");