2012-11-16 5 views
10

이상한 WebClient.UploadFileAsync()의 동작에 도움이 필요합니다. 파일을 원격 HTTP 서버 (nginx)의 POST 메소드로 업로드하고 있습니다. POST는 PHP 스크립트 (Address가 참조하는)를 통해 처리됩니다. WebClient UploadFileAsync 진행 중 이상한 동작보고

나는이 간단한 코드를

public void uploadFile(string filePath) 
{ 
    webClient = new WebClient(); 
    webClient.Credentials = new NetworkCredential(Constant.HTTPUsername,Constant.HTTPPassword); 
    webClient.Headers.Add("Test", TestKey); 
    webClient.UploadProgressChanged += webClient_UploadProgressChanged; 
    webClient.UploadFileCompleted += webClient_UploadFileCompleted; 

    try 
    { 
     webClient.UploadFileAsync(new Uri(Address), "POST", filePath); 
    } 
    catch (Exception error) 
    { 
     throw new CustomException(error.Message); 
    } 
} 

을 그리고 UploadProgressChanged에서 나는 단순히 주어진 ProgressPercentage와의 ProgressBar를 업데이트합니다. 첫 번째 문제는 모든 파일 크기가 함께 진행 비율,보고 된 것입니다 : 그래서

[17.38.14] Progress: 0 Bytes Sent: 175/269264 
[17.38.14] Progress: 1 Bytes Sent: 8367/269264 
[17.38.14] Progress: 3 Bytes Sent: 16559/269264 
[17.38.14] Progress: 4 Bytes Sent: 24751/269264 
[17.38.14] Progress: 6 Bytes Sent: 32943/269264 
[17.38.14] Progress: 7 Bytes Sent: 41135/269264 
[17.38.14] Progress: 9 Bytes Sent: 49327/269264 
[17.38.14] Progress: 10 Bytes Sent: 57519/269264 
[17.38.14] Progress: 12 Bytes Sent: 65711/269264 
[17.38.14] Progress: 13 Bytes Sent: 73903/269264 
[17.38.14] Progress: 15 Bytes Sent: 82095/269264 
[17.38.14] Progress: 16 Bytes Sent: 90287/269264 
[17.38.14] Progress: 18 Bytes Sent: 98479/269264 
[17.38.15] Progress: 19 Bytes Sent: 106671/269264 
[17.38.15] Progress: 21 Bytes Sent: 114863/269264 
[17.38.15] Progress: 22 Bytes Sent: 123055/269264 
[17.38.15] Progress: 24 Bytes Sent: 131247/269264 
[17.38.15] Progress: 25 Bytes Sent: 139439/269264 
[17.38.15] Progress: 27 Bytes Sent: 147631/269264 
[17.38.16] Progress: 28 Bytes Sent: 155823/269264 
[17.38.16] Progress: 30 Bytes Sent: 164015/269264 
[17.38.16] Progress: 31 Bytes Sent: 172207/269264 
[17.38.16] Progress: 33 Bytes Sent: 180399/269264 
[17.38.16] Progress: 35 Bytes Sent: 188591/269264 
[17.38.16] Progress: 36 Bytes Sent: 196783/269264 
[17.38.17] Progress: 38 Bytes Sent: 204975/269264 
[17.38.17] Progress: 39 Bytes Sent: 213167/269264 
[17.38.17] Progress: 41 Bytes Sent: 221359/269264 
[17.38.17] Progress: 42 Bytes Sent: 229551/269264 
[17.38.17] Progress: 44 Bytes Sent: 237743/269264 
[17.38.17] Progress: 45 Bytes Sent: 245935/269264 
[17.38.17] Progress: 47 Bytes Sent: 254127/269264 
[17.38.18] Progress: 48 Bytes Sent: 262319/269264 
[17.38.18] Progress: 49 Bytes Sent: 269220/269264 
[17.38.18] Progress: 50 Bytes Sent: 269264/269264 
[17.38.25] Progress: -50 Bytes Sent: 269264/269264 
[17.38.25] Progress: 100 Bytes Sent: 269264/269264 
[17.38.25] FileCompleted event raised! 

, 웹 검색, 내가 50-> 100에서 점프, 단지 디자인 선택 것으로 나타났습니다 퍼센트 보고서. 그리고 나는 그것으로 멋지다. 이상한 문제는 50 % (전체 파일 전송시)에도 네트워크 인터페이스가 여전히 트래픽을 생성하고 여전히 업로드 중임을 나타냅니다. 실제로 위의 로그에서 알 수 있듯이 UploadFileCompletedEvent를 발생 시키려면 파일을 보낸 후 7 초가 걸립니다. 사실 HTTP를 통해 파일을 보내는 중입니다.

문제점은 UI를 안정적으로 업데이트 할 수 없다는 것입니다. 진행률 표시 줄이 50 %까지 커지지 만 업로드 완료를 기다리는 데 걸리는 시간이 길어집니다. (큰 파일에서는 이번이 커지므로 좋지 않습니다. 크게).

질문 : 어떻게하면 파일 업로드 진행 상태를 사용자가 계속 신뢰할 수있게 유지할 수 있습니까?

감사합니다.

P. 이 메서드는 완벽하게 작동하고 파일은 원격 서버에 올바르게 업로드됩니다. 유일한 문제는 진행 상황보고입니다.

+0

모든 것이 OK 인 경우 솔루션을 대답으로 게시하고 동의하십시오. –

+0

때로는 여기에 질문을 올리는 것도 도움이됩니다. :) –

답변

2

방금 ​​문제를 발견했습니다 : 기본 HTTP 인증에 있습니다. 이상한 이유로, Credentials를 지정하더라도 WebClient는 HTTP 헤더에 자격 증명을 지정하지 않고 첫 번째 POST 요청을 제출합니다. 서버가 auth-request로 응답하면 자격 증명을 올바르게 지정하는 두 번째 POST 요청을 제출합니다. 이 두 번의 재시도 중, 내 응용 프로그램은 파일을 두 번 보냅니다!

string authInfo = userName + ":" + userPassword; 
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); 
req.Headers["Authorization"] = "Basic " + authInfo; 
:

이 솔루션은 수동 .. 라인을 인증 헤더를 추가 (그리고 WebClient.Credentials을 삭제하여 HTTP 기본 인증을 강제하는 것입니다 (내가 파일을 완전히 전송 된 후에도 업로드 활동을 경험하는 이유입니다)

이 방법은 첫 번째 (및 유일한) POST 요청이 인증 헤더와 함께 올바르게 제출되고 보고서 진행률이 올바르게 표시됩니다 (공유하기 위해 위의 이유로 인해 진행률 표시 줄에서 진행률을보고합니다.

1
string authInfo = userName + ":" + userPassword; 
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); 
req.Headers["Authorization"] = "Basic " + authInfo; 

진행 요청보고를 위해 게시물 요청 시작 부분에 이것을 e.ProgressPercentage * 2으로 지정하십시오.