2013-05-07 167 views
-1

我搜索了網絡,但無法找到任何方式獲得進展,而下載文件與HttpWebRequest。這個班級是否支持進步?任何鏈接,教程,提示將不勝感激。HttpWebRequest的下載文件和進度條

謝謝。

P.S.這裏是代碼...

private static Task<HttpResponse> MakeAsyncRequest(string requestString) 
    { 
     var request = (HttpWebRequest)WebRequest.Create(requestString); 
     Task<WebResponse> requestTask = Task.Factory.FromAsync(
      request.BeginGetResponse, 
      asyncResult => request.EndGetResponse(asyncResult), 
      null); 
     return requestTask.ContinueWith(t => ReadStreamFromResponce(t.Result)); 
    } 

    private static HttpResponse ReadStreamFromResponce(WebResponse result) 
    { 
     var responseobject = new HttpResponse(); 
     var response = (HttpWebResponse)result; 
     responseobject.StatusCode = (short)response.StatusCode; 

     if (!IsSuccess(responseobject.StatusCode)) 
      return responseobject; 

     using (var responseStream = response.GetResponseStream()) 
     using (var ms = new MemoryStream()) 
     { 
      responseStream.CopyTo(ms); 
      responseobject.SetResponse(ms.ToArray()); 
      return responseobject; 
     } 
    } 

回答

0

而是在同步拷貝響應流,儘量在異步的代表複製和鉤在下面的函數,你應該能夠獲得下載字節數的進步,顯示它的進步吧.. ..

public static void CopyToStreamAsync(this Stream source, Stream destination, 
     Action<Stream, Stream, Exception> completed, Action<uint> progress, 
     uint bufferSize, uint? maximumDownloadSize, TimeSpan? timeout) 
    { 
     byte[] buffer = new byte[bufferSize]; 

     Action<Exception> done = exception => 
      { 
       if (completed != null) 
       { 
        completed(source, destination, exception); 
       } 
      }; 

     int maxDownloadSize = maximumDownloadSize.HasValue 
      ? (int)maximumDownloadSize.Value 
      : int.MaxValue; 
     int bytesDownloaded = 0; 
     IAsyncResult asyncResult = source.BeginRead(buffer, 0, new[] { maxDownloadSize, buffer.Length }.Min(), null, null); 
     Action<IAsyncResult, bool> endRead = null; 
     endRead = (innerAsyncResult, innerIsTimedOut) => 
      { 
       try 
       { 
        int bytesRead = source.EndRead(innerAsyncResult); 
        if (innerIsTimedOut) 
        { 
         done(new TimeoutException()); 
        } 

        int bytesToWrite = new[] { maxDownloadSize - bytesDownloaded, buffer.Length, bytesRead }.Min(); 
        destination.Write(buffer, 0, bytesToWrite); 
        bytesDownloaded += bytesToWrite; 

        if (!progress.IsNull() && bytesToWrite > 0) 
        { 
         progress((uint)bytesDownloaded); 
        } 

        if (bytesToWrite == bytesRead && bytesToWrite > 0) 
        { 
         asyncResult = source.BeginRead(buffer, 0, new[] { maxDownloadSize, buffer.Length }.Min(), null, null); 
         // ReSharper disable PossibleNullReferenceException 
         // ReSharper disable AccessToModifiedClosure 
         asyncResult.FromAsync((ia, isTimeout) => endRead(ia, isTimeout), timeout); 
         // ReSharper restore AccessToModifiedClosure 
         // ReSharper restore PossibleNullReferenceException 
        } 
        else 
        { 
         done(null); 
        } 
       } 
       catch (Exception exc) 
       { 
        done(exc); 
       } 
      }; 

     asyncResult.FromAsync((ia, isTimeout) => endRead(ia, isTimeout), timeout); 
    } 
+0

謝謝,不幸的是我使用PCL(可移植類庫)項目,我們沒有Begin ****方法。你知道是否有可能在PCL中做類似的事情?謝謝 – Davita 2013-05-07 12:07:41