/// <summary> /// Initializes a new <c>FileDownloadProgress</c> object with the given values. /// </summary> /// <param name="state"></param> /// <param name="bytesDownloaded"></param> /// <param name="contentLength"></param> /// <param name="bitsPerSecond"></param> public FileDownloadProgress(FileDownloadState state, int bytesDownloaded, long contentLength, double bitsPerSecond) { State = state; BytesDownloaded = bytesDownloaded; ContentLength = contentLength; BitsPerSecond = bitsPerSecond; BytesPerSecond = BitsPerSecond / 8; PercentComplete = 100.0 * ((double)BytesDownloaded / ContentLength); HumanSpeed = string.Format("{0}/s", FileUtils.HumanFriendlyFileSize((long)BytesPerSecond)); IsComplete = (BytesDownloaded == ContentLength); }
/// <summary> /// Initializes a new <c>FileDownloadProgress</c> object with the given values. /// </summary> /// <param name="state"></param> /// <param name="bytesDownloaded"></param> /// <param name="contentLength"></param> /// <param name="bitsPerSecond"></param> public FileDownloadProgress(FileDownloadState state, int bytesDownloaded, long contentLength, double bitsPerSecond) { State = state; BytesDownloaded = bytesDownloaded; ContentLength = contentLength; BitsPerSecond = bitsPerSecond; BytesPerSecond = BitsPerSecond / 8; PercentComplete = 100.0 * ((double)BytesDownloaded / ContentLength); HumanSpeed = string.Format("{0}/s", FileUtils.HumanFriendlyFileSize((long)BytesPerSecond)); IsComplete = (BytesDownloaded == ContentLength); }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> public void DownloadFileAsync(string source, FileStream destination) { var _requestMethod = WebRequestMethod.DownloadFile; var _resourceName = WebRequestHelpers.ReplacePathDelimiterToUriDelimiter(source); var _uri = CreateUniformResourceIdentifier(_resourceName); var _request = CreateRequest(_uri, _requestMethod); var _length = GetContentLength(_uri); var _state = new FileDownloadState(_request, BUFFER_SIZE, null, destination, _length); _request.BeginGetResponse(new AsyncCallback(_BeginGetResponseCallback), _state); }
/// <summary> /// /// </summary> /// <param name="asyncResult"></param> void _BeginGetResponseCallback(IAsyncResult asyncResult) { var _state = asyncResult.AsyncState as FileDownloadState; try { var _response = _state.Request.EndGetResponse(asyncResult); var _source = _response.GetResponseStream(); var _newState = new FileDownloadState(_state.Request, _state.BufferSize, _source, _state.Destination, _state.ContentLength); var _callback = new AsyncCallback(_BeginReadSourceCallback); _newState.Source.BeginRead(_newState.ByteBuffer, 0, _newState.ByteBuffer.Length, _callback, _newState); } catch (Exception ex) { _state.Dispose(); RaiseDownloadFaultedEvent(ex); } }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <returns></returns> public IExternalResult DownloadFile(string source, FileStream destination) { var _requestMethod = WebRequestMethod.DownloadFile; var _resourceName = WebRequestHelpers.ReplacePathDelimiterToUriDelimiter(source); var _uri = CreateUniformResourceIdentifier(_resourceName); var _request = CreateRequest(_uri, _requestMethod); var _length = GetContentLength(_uri); var _response = _request.GetResponse(); var _source = _response.GetResponseStream(); using (var _state = new FileDownloadState(_request, BUFFER_SIZE, _source, destination, _length)) { while (0 < (_state.BytesDownloaded = _state.Source.Read(_state.ByteBuffer, 0, _state.ByteBuffer.Length))) { _state.Destination.Write(_state.ByteBuffer, 0, _state.BytesDownloaded); } return(GetRequestResult(_response, _requestMethod)); } }