예제 #1
0
        /// <summary>
        /// Upload file
        /// </summary>
        /// <returns>Awaitable task object</returns>
        private static async Task UploadFile()
        {
            try
            {
                string uploadRequestURI = "uploadasync?overWrite=true";

                MultipartFormDataContent formDataContent = new MultipartFormDataContent();

                // Validate the file and add to MultipartFormDataContent object
                formDataContent.AddUploadFile(@"c:\nophoto.png");
                formDataContent.AddUploadFile(@"c:\ReadMe.txt");
                // Add more file
                if (!formDataContent.HasContent()) // No files found to be uploaded
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(formDataContent.GetUploadFileErrorMesage());
                    return;
                }
                else
                {
                    string uploadErrorMessage = formDataContent.GetUploadFileErrorMesage();
                    if (!string.IsNullOrWhiteSpace(uploadErrorMessage)) // Some files couldn't be found
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(uploadErrorMessage);
                    }

                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uploadRequestURI);
                    request.Content = formDataContent;

                    using (HttpClient httpClient = new HttpClient())
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine(string.Format("Start uploading @ {0}, {1} time ",
                        DateTime.Now.ToLongDateString(),
                        DateTime.Now.ToLongTimeString()));

                        httpClient.BaseAddress = baseStreamingURL;
                        await httpClient.SendAsync(request).
                              ContinueWith((response)
                                  =>
                                  {
                                      try
                                      {
                                          ProcessUploadResponse(response);
                                      }
                                      catch (AggregateException aggregateException)
                                      {
                                          Console.ForegroundColor = ConsoleColor.Red;
                                          Console.WriteLine(string.Format("Exception : ", aggregateException));
                                      }
                                  });
                    }
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
            }
        }