1. Simone Olianti
  2. SnapDevelop
  3. Wednesday, 13 May 2020 09:20 AM UTC

Hello, i've successfully created a small webapi used to communicate from a PB client application and exchange data to a remote asa database. The PB (2019R2) client app is using restclient to send and get data from a table and everything working great.
Next step i would like to send a small image grabbed from an inkpicture control to the webapi and save it to a long binary table field.
I was able to convert the blob coming from ink control to a base64 string and sent it to the web api using a sendpostrequest.
I'm not yet very familiar with C# so now my question is: is it possible to convert back the base64 string to a blob and save it to remote table?
i guess i should use PbBlob class and SqlUpdateBuilder to construct the statement and execute it from the web api but i am not sure how to do it, if it is the best choice or if is there a better approach to accomplish this task

any help or suggestion would be appreciated

tia,

  simone

Accepted Answer
Marco Meoni Accepted Answer Pending Moderation
  1. Wednesday, 13 May 2020 11:11 AM UTC
  2. SnapDevelop
  3. # Permalink

Hi Simone,

you can use C# System.Convert.FromBase64CharArray() to convert your input Base64-encoded-string to byte array. That's your "blob" ready to be stored into the DB.

Best,

.m

Comment
  1. Thomas Rolseth
  2. Tuesday, 22 June 2021 14:48 PM UTC
Simone,

Any chance you could share your PB code that calls sendpostrequest as well as your controller and service code in SnapDevelop? I have a similar situation with a project involving sending large binary files to the middle tier for processing.
  1. Helpful
  1. Simone Olianti
  2. Tuesday, 29 June 2021 09:13 AM UTC
Simone Olianti

Hi Thomas, it's quite simple and here it is:



//converting the source ink blob into base64 string

string img

lnv_CoderObject = Create CoderObject

img= lnv_CoderObject.Base64Encode(lblb_ink)

destroy lnv_CoderObject



//calling web api passing the base64 string which execute the code i posted above

RESTClient inv_RestClient

inv_RestClient = CREATE RESTClient

inv_RESTClient.SetRequestHeader ("Content-Type", "application/json;charset=UTF-8")

inv_RestClient.Setrequestheader( "Accept-Encoding", "gzip")



string ls_id

ls_id = string(dw_1.getitemnumber( dw_1.getrow(), "id_interventi"))

inv_RestClient.setrequestheader( "id_interventi", ls_id)

ll_rtn = inv_RestClient.sendpostrequest(ls_Request_Url, img, ls_response)

if inv_RestClient.GetResponseStatusCode() <> 200 then

ls_msg = "Status Code: " + String(inv_RestClient.GetResponseStatusCode()) + '~r~n' + "Status Text: " + String(inv_RestClient.GetResponseStatusText()) + '~r~n'

Messagebox("Error - Response Info", ls_msg, stopsign!, ok!)

end if
  1. Helpful
  1. Thomas Rolseth
  2. Thursday, 29 July 2021 21:14 PM UTC
Thanks Simone. What if the image you are encoding is very large? In my case I want to apply Base64Encode to a 10MB+ PDF file. I'm guessing that won't work since I think a string variable can only be 32k or less. Also, what is the value of ls_request_url in this example? I'm getting a 400 Bad request error when I call into an API controller that I setup similar to yours. Tom
  1. Helpful
There are no comments made yet.
Alessandro Bracco Accepted Answer Pending Moderation
  1. Tuesday, 29 June 2021 08:40 AM UTC
  2. SnapDevelop
  3. # 1

Hi Simone

Any chance you could share the Service Code in SnapDevelope ?! I think what you did is very helpful and cool!

Thanks a lot

Ale

 

 

Comment
  1. Simone Olianti
  2. Tuesday, 29 June 2021 09:24 AM UTC
Ciao Ale, i've create a service using SqlModelMapper and here is the code:



//service

using System;

namespace StartWebApi3.Services.SqlModelMapper

{

public interface IMMASAInterventiWebInkService

{

int UpdateInk(string img, string id);

String GetConnection();

}

}



//Impl (service)

using System;

using SnapObjects.Data;

namespace StartWebApi3.Services.SqlModelMapper.Impl

{

public class MMASAInterventiWebInkService : IMMASAInterventiWebInkService

{

private readonly DataContextBaseSqlModelMapperAsa _dataContext;

public MMASAInterventiWebInkService(DataContextBaseSqlModelMapperAsa dataContext)

{

_dataContext = dataContext;

}



public int UpdateInk(string img, string id)

{

byte[] decodedByteArray = Convert.FromBase64String(img);

var sqlbuilder = new SqlUpdateBuilder();

sqlbuilder.Update("interventi").SetValue("firma_cliente", decodedByteArray).Where("id_interventi", id);



// Converts to raw SQL for the database corresponding to the data context.

string sql = sqlbuilder.ToSqlString(_dataContext);

Console.WriteLine(sql);

int row = _dataContext.SqlExecutor.Execute(sql);

return 1;

}



public String GetConnection()

{

// Current DataContext's ConnectionString

return _dataContext.CurrentConnection.ConnectionString;

}

}

}



//Controller

using StartWebApi3.Services.SqlModelMapper;

using Microsoft.AspNetCore.Mvc;

using System;

using Microsoft.AspNetCore.Authorization;



namespace StartWebApi3.Controllers.SqlModelMapper

{

[Route("api/[controller]/[action]")]

[ApiController]

public class MMASAInterventiWebInkController : ControllerBase

{

private IMMASAInterventiWebInkService _service;



public MMASAInterventiWebInkController(IMMASAInterventiWebInkService service)

{

_service = service;

}



[HttpPost]

[Authorize]

public int UpdateInk([FromBody]string img, [FromHeader]string id_interventi)

{

string id = Request.Headers["id_interventi"];

return _service.UpdateInk(img, id);

}



[HttpGet]

public String GetConnection()

{

// Current DataContext's ConnectionString

return _service.GetConnection();

}

}

}



Hope that helps!
  1. Helpful
  1. Alessandro Bracco
  2. Wednesday, 30 June 2021 07:14 AM UTC
Hi Simone





Thank you very much, I immediately start working around your example, very very useful.

Thanks again

Ale

  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.