public static async Task UploadAsync(UploadArguments arguments) { var process = Enumeration.FromDisplayName <Process>(arguments.ProcessName); if (process.Type != Process.ProcessType.Upload) { throw new ArgumentException($"Process {arguments.ProcessName} can not be uploaded.", nameof(process)); } var apiEndpoint = new Uri(arguments.ApiEndpoint); await using (var fileStream = File.OpenRead(arguments.Filename)) using (var client = GetClient(apiEndpoint, arguments.Token)) using (var streamContent = new StreamContent(fileStream)) using (var content = new MultipartFormDataContent()) { content.Add(streamContent, "file", arguments.Filename); var response = await client.PostAsync(process.Name, content, CancellationToken.None).ConfigureAwait(false); if (response.IsSuccessStatusCode) { Console.WriteLine($"Upload of file {arguments.Filename} to API endpoint {new Uri(apiEndpoint, arguments.ProcessName)}"); } else { await PrintErrorAsync(response, new Uri(apiEndpoint, arguments.ProcessName), process.Type, CancellationToken.None).ConfigureAwait(false); } } }
void backgroundWorkerUploadImage_DoWork(object sender, DoWorkEventArgs e) { UploadArguments arguments = e.Argument as UploadArguments; API.ImageServices.ImageResponse uploadedUrl = null; uploadedUrl = AppController.Current.ActualImageService.Upload(arguments.filepath, arguments.account); if (uploadedUrl.Success) { backgroundWorkerUploadImage.ReportProgress(100, uploadedUrl.UrlFull); } else { MessageBox.Show(uploadedUrl.ErrorText, "Error in image upload"); backgroundWorkerUploadImage.ReportProgress(100, ""); } }
void backgroundWorkerUploadImage_DoWork(object sender, DoWorkEventArgs e) { UploadArguments arguments = e.Argument as UploadArguments; if (System.IO.File.Exists(arguments.filepath)) { Tuple <File, ApiCallResponse> response = Files.create(arguments.account.accessToken, arguments.filepath, type: "com.nymphicusapp.image"); if (response.Item2.success) { backgroundWorkerUploadImage.ReportProgress(100, response.Item1); } else { backgroundWorkerUploadImage.ReportProgress(0, response.Item2.errorMessage); } } }