private static void DownLoadFinishedCallback(IAsyncResult ar) { HttpAsyncDownLoader state = (HttpAsyncDownLoader)ar.AsyncState; try { WebResponse response = state._request.EndGetResponse(ar); state._response = response; Stream stream = response.GetResponseStream(); long contentLength = response.ContentLength; long currentSize = state._startPos; byte[] bytes = new byte[BUFFER_SIZE]; int readSize = stream.Read(bytes, 0, bytes.Length); currentSize += readSize; while (readSize > 0) { state._fileStream.Write(bytes, 0, readSize); readSize = stream.Read(bytes, 0, bytes.Length); currentSize += readSize; int percent = (int)((double)currentSize * 100 / contentLength); if (state.percent < percent) { state.percent = percent; } } state._fileStream.Flush(); stream.Close(); state.Dispose(); if (state.downLoadFinishedCallback != null) { state.downLoadFinishedCallback(true); } } catch (Exception e) { state.Dispose(); FileInfo info = new FileInfo(state.nativePath); if (info.Exists && info.Length == 0) { info.Delete(); } if (state.downLoadFinishedCallback != null) { state.downLoadFinishedCallback(false); } if (state.errorCallback != null) { state.errorCallback(e); } } }
private void Start() { if (File.Exists(_nativePath)) { File.Delete(_nativePath); } _downLoader = new HttpAsyncDownLoader(_url, _nativePath, ErrorCallback, DownLoadFinishedCallback); _downLoader.Start(); }