private async static Task<string> GetResponseMessage(UploadOperation uploadOperation) { ResponseInformation responseInformation = uploadOperation.GetResponseInformation(); //uint contentLength = Convert.ToUInt32(responseInformation.Headers["Content-Length"]); uint contentLength = (uint)uploadOperation.Progress.BytesReceived; uint statusCode = responseInformation.StatusCode; IInputStream resultStreamAt = uploadOperation.GetResultStreamAt(0); IBuffer result = await resultStreamAt.ReadAsync( new Windows.Storage.Streams.Buffer(contentLength), contentLength, InputStreamOptions.None); Stream responseStream = result.AsStream(); XDocument xDocument = XDocument.Load(responseStream); foreach (XElement xe in xDocument.Root.Descendants()) { if (xe.Name.LocalName.Equals("Ok")) { return "SUCCESS"; } if (xe.Name.LocalName.Equals("Error")) { return xe.Value; } } return "Unspecified error submiting video"; }
private async Task HandleUploadAsync(UploadOperation upload, bool start) { try { LogStatus("Running: " + upload.Guid, NotifyType.StatusMessage); Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgress); if (start) { // Start the upload and attach a progress handler. await upload.StartAsync().AsTask(cts.Token, progressCallback); } else { // The upload was already running when the application started, re-attach the progress handler. await upload.AttachAsync().AsTask(cts.Token, progressCallback); } ResponseInformation response = upload.GetResponseInformation(); LogStatus(String.Format(CultureInfo.CurrentCulture, "Completed: {0}, Status Code: {1}", upload.Guid, response.StatusCode), NotifyType.StatusMessage); } catch (TaskCanceledException) { LogStatus("Canceled: " + upload.Guid, NotifyType.StatusMessage); } catch (Exception ex) { if (!IsExceptionHandled("Error", ex, upload)) { throw; } } }
// Note that this event is invoked on a background thread, so we cannot access the UI directly. private void UploadProgress(UploadOperation upload) { MarshalLog(String.Format(CultureInfo.CurrentCulture, "Progress: {0}, Status: {1}", upload.Guid, upload.Progress.Status)); BackgroundUploadProgress progress = upload.Progress; double percentSent = 100; if (progress.TotalBytesToSend > 0) { percentSent = progress.BytesSent * 100 / progress.TotalBytesToSend; } MarshalLog(String.Format(CultureInfo.CurrentCulture, " - Sent bytes: {0} of {1} ({2}%), Received bytes: {3} of {4}", progress.BytesSent, progress.TotalBytesToSend, percentSent, progress.BytesReceived, progress.TotalBytesToReceive)); if (progress.HasRestarted) { MarshalLog(" - Upload restarted"); } if (progress.HasResponseChanged) { // We've received new response headers from the server. MarshalLog(" - Response updated; Header count: " + upload.GetResponseInformation().Headers.Count); // If you want to stream the response data this is a good time to start. // upload.GetResultStreamAt(0); } }
private async Task HandleUploadAsync(UploadOperation upload, bool start) { try { CancellationTokenSource cts = new CancellationTokenSource(); Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(); if (start) { // Start the upload and attach a progress handler. await upload.StartAsync().AsTask(cts.Token, progressCallback); } else { // The upload was already running when the application started, re-attach the progress handler. await upload.AttachAsync().AsTask(cts.Token, progressCallback); } ResponseInformation response = upload.GetResponseInformation(); // Log(String.Format("Completed: {0}, Status Code: {1}", upload.Guid, response.StatusCode)); } catch (TaskCanceledException) { // Log("Upload cancelled."); } catch (Exception ex) { //LogException("Error", ex); } }
// Note that this event is invoked on a background thread, so we cannot access the UI directly. private void UploadProgress(UploadOperation upload) { // UploadOperation.Progress is updated in real-time while the operation is ongoing. Therefore, // we must make a local copy at the beginning of the progress handler, so that we can have a consistent // view of that ever-changing state throughout the handler's lifetime. BackgroundUploadProgress currentProgress = upload.Progress; MarshalLog(String.Format(CultureInfo.CurrentCulture, "Progress: {0}, Status: {1}", upload.Guid, currentProgress.Status)); double percentSent = 100; if (currentProgress.TotalBytesToSend > 0) { percentSent = currentProgress.BytesSent * 100 / currentProgress.TotalBytesToSend; } MarshalLog(String.Format(CultureInfo.CurrentCulture, " - Sent bytes: {0} of {1} ({2}%), Received bytes: {3} of {4}", currentProgress.BytesSent, currentProgress.TotalBytesToSend, percentSent, currentProgress.BytesReceived, currentProgress.TotalBytesToReceive)); if (currentProgress.HasRestarted) { MarshalLog(" - Upload restarted"); } if (currentProgress.HasResponseChanged) { // We've received new response headers from the server. MarshalLog(" - Response updated; Header count: " + upload.GetResponseInformation().Headers.Count); // If you want to stream the response data this is a good time to start. // upload.GetResultStreamAt(0); } }
/// <summary> /// Start uplaod and create handler. /// </summary> /// <param name="upload">UploadOperation handled.</param> /// <param name="start">Whether uplaod is started.</param> /// <returns> /// Represent the asynchronous operation. /// </returns> private async Task HandleUploadAsync(UploadOperation upload, bool start) { try { try { Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgress); if (start) { // Start the upload and attach a progress handler. await upload.StartAsync().AsTask(cts.Token, progressCallback); } else { // The upload was already running when the application started, re-attach the progress handler. await upload.AttachAsync().AsTask(cts.Token, progressCallback); } } catch { ShowMessageDialog("Error90! Upload canceled."); } ResponseInformation response = upload.GetResponseInformation(); foreach (var c in response.Headers) { System.Diagnostics.Debug.WriteLine("{0}, {1}. markkkkkkkk", c.Key, c.Value); } if (images != null && images.Count != 0 && hasImage == false) { toBeUploadCourse.ImageUri = response.Headers[images.FirstOrDefault().Name]; hasImage = true; } SaveUploadLessonToListAsync(response); } catch (TaskCanceledException) { ShowMessageDialog("Error9! Upload canceled."); } catch { ShowMessageDialog("Error10! Upload canceled."); //throw; } }