/// <summary> /// /// </summary> /// <param name="exception"></param> /// <param name="state"></param> void RaiseUploadFaultedEvent(Exception exception, FileUploadAsyncState state) { var _argument = new UploadFailedEventArgs(exception, state.UserToken); if (null != UploadFailed) { UploadFailed(this, _argument); } }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> public void UploadFileAsync(Stream source, string destination, object userToken = null) { var _requestMethod = WebRequestMethod.UploadFile; var _directoryName = Path.GetDirectoryName(destination); if (!string.IsNullOrEmpty(_directoryName)) { CreateDirectory(_directoryName); } var _uri = CreateUniformResourceIdentifier(WebRequestHelpers.ReplacePathDelimiterToUriDelimiter(destination)); var _request = CreateRequest(_uri, _requestMethod); var _state = new FileUploadAsyncState(_request, BUFFER_SIZE, source, null, source.Length, userToken); _request.BeginGetRequestStream(new AsyncCallback(_BeginGetRequestStream), _state); }
/// <summary> /// /// </summary> /// <param name="asyncResult"></param> void _BeginGetRequestStream(IAsyncResult asyncResult) { var _state = asyncResult.AsyncState as FileUploadAsyncState; try { var _destination = _state.Request.EndGetRequestStream(asyncResult); var _newState = new FileUploadAsyncState(_state.Request, _state.BufferSize, _state.Source, _destination, _state.ContentLength, _state.UserToken); var _callback = new AsyncCallback(_BeginReadSourceCallback); _newState.Source.BeginRead(_newState.ByteBuffer, 0, _newState.ByteBuffer.Length, _callback, _newState); } catch (Exception ex) { RaiseUploadFaultedEvent(ex, _state); _state.Dispose(); } }