/// <summary> /// Upload a file to the server. /// </summary> /// <param name="path">relative or absolute uri to the location where the file should be uploaded to.</param> /// <param name="fileName">name for the uploaded file.</param> /// <param name="inputStream">Stream that contains the file content.</param> /// <param name="option"> /// a enum to specify the overwrite behavior if a file with the same name already exists. /// Default is DoNotOverwrite. /// </param> /// <param name="ct">a cancellation token</param> /// <param name="progress">a progress event callback handler</param> public Task<LiveOperationResult> UploadAsync( string path, string fileName, Stream inputStream, OverwriteOption option, CancellationToken ct, IProgress<LiveOperationProgress> progress) { LiveUtility.ValidateNotNullOrWhiteSpaceString(path, "path"); LiveUtility.ValidateNotNullParameter(inputStream, "inputStream"); if (string.IsNullOrEmpty(fileName)) { string message = String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("InvalidNullOrEmptyParameter"), "fileName"); throw new ArgumentException(message, "fileName"); } if (!inputStream.CanRead) { string message = String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("StreamNotReadable"), "inputStream"); throw new ArgumentException(message, "inputStream"); } if (this.Session == null) { throw new LiveConnectException(ApiOperation.ApiClientErrorCode, ResourceHelper.GetString("UserNotLoggedIn")); } var tcs = new TaskCompletionSource<LiveOperationResult>(); var op = new UploadOperation( this, this.GetResourceUri(path, ApiMethod.Upload), fileName, inputStream, option, progress, null); op.OperationCompletedCallback = (LiveOperationResult result) => { if (result.IsCancelled) { tcs.TrySetCanceled(); } else if (result.Error != null) { tcs.TrySetException(result.Error); } else { tcs.TrySetResult(result); } }; ct.Register(op.Cancel); op.Execute(); return tcs.Task; }
/// <summary> /// Upload a file to the server. /// </summary> /// <param name="path">relative or absolute uri to the location where the file should be uploaded to.</param> /// <param name="fileName">name for the uploaded file.</param> /// <param name="inputStream">Stream that contains the file content.</param> /// <param name="option">an enum to specify the overwrite behavior if a file with the same name already exists.</param> /// <param name="ct">a token that is used to cancel the upload operation.</param> /// <param name="progress">an object that is called to report the upload's progress.</param> /// <returns>A Task object representing the asynchronous operation.</returns> public Task<LiveOperationResult> UploadAsync( string path, string fileName, Stream inputStream, OverwriteOption option, CancellationToken ct, IProgress<LiveOperationProgress> progress) { if (string.IsNullOrEmpty(path)) { string message = String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("UrlInvalid"), "path"); throw new ArgumentException(message, "path"); } if (string.IsNullOrEmpty(fileName)) { string message = String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("InvalidNullOrEmptyParameter"), "fileName"); throw new ArgumentException(message, "fileName"); } if (inputStream == null) { string message = String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("InvalidNullParameter"), "inputStream"); throw new ArgumentNullException("inputStream", message); } if (!inputStream.CanRead) { string message = String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("StreamNotReadable"), "inputStream"); throw new ArgumentException(message, "inputStream"); } var operation = new UploadOperation( this, this.GetResourceUri(path, ApiMethod.Upload), fileName, inputStream, option, progress != null ? new Action<LiveOperationProgress>(progress.Report) : null, SynchronizationContextWrapper.Current); return this.ExecuteApiOperation(operation, ct); }