public object PostBlobCall(ServerCallParameters parameters, object targetObject, object parent, object data, IEnumerable <IUploadableFile> files) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (files == null) { throw new ArgumentNullException(nameof(files)); } string sdata = SerializeData(data); using (var httpHandler = new HttpClientHandler()) using (var client = new HttpClient(httpHandler)) { if (Cookie != null) { httpHandler.CookieContainer.Add(new Cookie(CookieName, Cookie, "/", new Uri(Url).Host)); } var uri = new EditableUri(Url + "/api/" + parameters.Api); if (parameters.Lcid != 0) { uri.Parameters["l"] = parameters.Lcid; } var mpfdContent = new MultipartFormDataContent(); if (sdata != null) { mpfdContent.Add(new StringContent(sdata, Encoding.UTF8, "application/json"), "data"); } foreach (var blob in files) { if (string.IsNullOrEmpty(blob.TempFilePath)) { continue; } var streamContent = new StreamContent(File.Open(blob.TempFilePath, FileMode.Open, FileAccess.Read)); streamContent.Headers.ContentType = new MediaTypeHeaderValue(blob.ContentType); mpfdContent.Add(streamContent, blob.FormName, ConvertUtilities.RemoveDiacritics(blob.FileName)); } var message = new HttpRequestMessage(HttpMethod.Post, uri.ToString()); message.Content = mpfdContent; string s; try { var result = client.SendAsync(message).Result; s = result.Content.ReadAsStringAsync().Result; result.EnsureSuccessStatusCode(); } catch (AggregateException agg) { if (ShowMessageBoxOnError) { agg.Handle(e => { var we = e as WebException; if (we != null) { var eb = new ErrorBox(e, e.GetErrorText(null)); eb.ShowDialog(); } return(false); }); } throw; } catch (HttpRequestException e) { if (ShowMessageBoxOnError) { var eb = new ErrorBox(e, e.GetErrorText(null)); eb.ShowDialog(); } throw; } var options = new JsonUtilitiesOptions(); options.CreateInstanceCallback = (e) => { var type = (Type)e.Value; if (typeof(TreeItem).IsAssignableFrom(type)) { e.Value = Activator.CreateInstance(type, new object[] { parent }); e.Handled = true; } }; if (targetObject != null) { JsonUtilities.Deserialize(s, targetObject, options); return(null); } return(JsonUtilities.Deserialize(s)); } }
public static void ShowErrorBox(string error) { ErrorBox errorBox = new ErrorBox(error); errorBox.ShowDialog(); }
public object Call(ServerCallParameters parameters, object targetObject, object parent) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } using (var client = new CookieWebClient()) { if (Cookie != null) { client.Cookies.Add(new Cookie(CookieName, Cookie, "/", new Uri(Url).Host)); } var uri = new EditableUri(Url + "/api/" + parameters.Api); if (!string.IsNullOrWhiteSpace(parameters.Format)) { uri.Parameters["f"] = parameters.Format; } if (parameters.Lcid != 0) { uri.Parameters["l"] = parameters.Lcid; } client.Encoding = Encoding.UTF8; string s; try { s = client.DownloadString(uri.ToString()); } catch (WebException e) { if (ShowMessageBoxOnError) { var eb = new ErrorBox(e, e.GetErrorText(null)); eb.ShowDialog(); } throw; } var options = new JsonUtilitiesOptions(); options.CreateInstanceCallback = (e) => { Type type = (Type)e.Value; if (typeof(TreeItem).IsAssignableFrom(type)) { e.Value = Activator.CreateInstance(type, new object[] { parent }); e.Handled = true; } }; if (targetObject != null) { JsonUtilities.Deserialize(s, targetObject, options); return(null); } return(JsonUtilities.Deserialize(s)); } }