Question

Use the PutFile method to save base64 string as an attachment

  • 7 November 2023
  • 0 replies
  • 30 views

Userlevel 2
Badge

I’m trying to use the PutFile method (https://help-2020r2.acumatica.com/(W(8))/Help?ScreenId=ShowWiki&pageid=591def3d-5fe5-42c1-bb35-cfe5b4c77f6f)

I am using version 23.109.0023 and here is my code:

 

        public async Task HandleAsync(WebhookContext context, CancellationToken cancellation)
{
using (var scope = GetAdminScope())
{
try
{
if (context.Request.Method == HttpMethod.Post.Method)
{
using (var result = new JsonTextReader(context.Request.CreateTextReader()))
{
if (result.Read())
{
JsonSerializer serializer = new JsonSerializer();
ObjectJSON test = serializer.Deserialize<ObjectJSON>(result);

var bytes = Convert.FromBase64String(test.Value);
StreamContent contents = new StreamContent(new MemoryStream(bytes));
using( RestService rs = new RestService("http://localhost", "APIUser", "********", "HBH Test", "HOJ", ""))
{
string fileAttachResult = rs.PutFile("StockItem", "733-108", "test.txt", contents);
}
}
}
}
return;
}
catch (Exception ex)
{
return;
}
}
}
        public string PutFile(string entityName, string keys, string fileName, StreamContent file) 
{
try
{
var res = _httpClient.PutAsync(_acumaticaBaseUrl + "/entity/Default/22.200.001/" +
entityName + "/" + keys + "/files/" + fileName, file).Result.EnsureSuccessStatusCode();
return res.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
return ex.Message;
}
}

When I run the code it always goes to the catch exception with this:

System.AggregateException: One or more errors occurred. ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at WebhookRedList.RestService.PutFile(String entityName, String keys, String fileName, StreamContent file)
---> (Inner Exception #0) System.Threading.Tasks.TaskCanceledException: A task was canceled.<---
 

I’m not sure what I’m missing or how to get anymore information on the errors.  Any help will be much apprecieated.


0 replies

Be the first to reply!

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved