public override void BeginUpload(UploadEventArgs args) { if (args == null) { throw new ArgumentNullException("args"); } try { _uploadEventArgs = args; _uploadEventArgs.UploadItem.SetResumeAction(ProcessUpload); _uploadStream = args.UploadItem.FileInfo.OpenRead(); _uploadEventArgs.BeginUpload(); } catch (Exception ex) { Exception = new UploadException("Response Callback Exception", ex); _uploadEventArgs.UploadCompleted(Exception); } }
public static async Task UploadFailed(UploadException result) { if (result.InnerException is UnableToFillTemplateException templateException) { var tempFile = System.IO.Path.GetTempFileName(); await System.IO.File.WriteAllTextAsync(tempFile, templateException.ServerResponse); var page = new TaskDialogPage() { Text = $"The provided \"urlTemplate\" didn't work. It raised the following error:\n\n{templateException.Message}\n\nWe've saved the server's response to this file, so you can investigate the issue:\n\n${tempFile}", Footnote = new TaskDialogFootnote() { Text = "Pro tip: You can press Ctrl+C while having this dialog focused to copy its contents.", }, }; TaskDialog.ShowDialog(page); return; } Show("Error Uploading Image", string.Empty, result.Message, TaskDialogIcon.Error, TaskDialogButton.OK); }
static void DigitalOceanUploadManager_UploadExceptionEvent(object sender, UploadException e) { Console.WriteLine(e.Message); }
private void ReadHttpResponseCallback(IAsyncResult asynchronousResult) { long lastPosition = 0; bool isCompleted = false; try { if (_uploadEventArgs.UploadItem.Status == FileUploadStatus.Canceled) { isCompleted = true; } else { _isUploading = false; ChunkState chunkState = (ChunkState) asynchronousResult.AsyncState; lastPosition = chunkState.Position; TimeSpan uploadDuration = DateTime.Now - chunkState.StartTime; string responseString = null; using (HttpWebResponse webResponse = (HttpWebResponse) chunkState.Request.EndGetResponse(asynchronousResult)) { StreamReader reader = new StreamReader(webResponse.GetResponseStream()); responseString = HttpUtility.HtmlDecode(reader.ReadToEnd()); } CheckFileUploadStatus(responseString); if (Exception == null) { _uploadEventArgs.UploadProgressChanged(_totalBytesUploaded - lastPosition, uploadDuration); if (_totalBytesUploaded < _uploadEventArgs.UploadItem.Length) { if (_uploadEventArgs.UploadItem.Status == FileUploadStatus.Uploading) { ProcessUpload(); } } else { isCompleted = true; } } else { if (_uploadEventArgs.UploadItem.CanRetry()) { TryFailedUpload(lastPosition); } else { isCompleted = true; } } } } catch (Exception ex) { if (_uploadEventArgs.UploadItem.CanRetry()) { TryFailedUpload(lastPosition); } else { isCompleted = true; Exception = new UploadException("Response Callback Exception", ex); } } finally { if (isCompleted) { _uploadEventArgs.UploadCompleted(Exception); } } }
protected void CheckFileUploadStatus(string response) { UploadInfo info = DeserializeUploadResponse(response); if (info != null) { string errorMessage = HttpUtility.HtmlDecode(info.ErrorMessage); HttpUploadCode code = (HttpUploadCode) info.StatusCode; switch (code) { case HttpUploadCode.Ok: { break; } case HttpUploadCode.Unauthorized: { Exception = new UnauthorizedException(errorMessage); break; } case HttpUploadCode.NotFound: { Exception = new FileNotFoundException(errorMessage); break; } case HttpUploadCode.FileExists: { Exception = new FileExistsException(errorMessage); break; } case HttpUploadCode.BlockedFile: { Exception = new FileBlockedException(errorMessage); break; } case HttpUploadCode.FileLocked: { Exception = new FileLockedException(errorMessage); break; } case HttpUploadCode.DeleteFailed: { Exception = new DeleteFailedException(errorMessage); break; } default: { Exception = new UploadException(string.Format(CultureInfo.InvariantCulture, "Invalid status code : {0}", code)); break; } } } }
protected UploadInfo DeserializeUploadResponse(string response) { UploadInfo info = null; try { info = JsonSerializer.Deserialize<UploadInfo>(response); } catch (Exception ex) { Exception = new UploadException("Unable to deserialize upload response", ex); } return info; }