1. Alex Riofrio
  2. SnapObjects
  3. Thursday, 4 February 2021 17:41 PM UTC

Hello everyone;

I'm testing sending an image from a Web API (SnapDevelop 2019 R3) to a PowerBuilder 2019 R3 app.
When I use the byte[] data type and then sending it inside a DataPacker, the sending and receiving works fine.

When I do a similar test with PbBlob data type it doesn't work properly. Does anyone have a simple example of how to send a PbBlob and how to get it in PB 2019?

 

Note: I've tried changing the PbBlob variable, in the Controller, to String and then sending it to PB, but it doesn't work either.

Thanks in advance.

 

Alex

 

Accepted Answer
David Xiong @Appeon Accepted Answer Pending Moderation
  1. Friday, 5 February 2021 10:11 AM UTC
  2. SnapObjects
  3. # Permalink

Hi Alex,

I analyzed your code and the issue is with the code parsing in PB Client.

Since the result set format sent from Web API to PowerBuilder is:

{
    "photo": {
        "Size": 21331,
        "Length": 21331,
        "Value": "R0lGODlh8"  

           }
}

It still needs to parse it after retrieving the photo value and the value of the “Value” Key is what we need.

 

There are two methods below to retrieve the value of the “Value” Key:

Method #1: Parse several times using the JSONPackage method to get the value of the “Value” Key (I added two lines of code marked in red based on your code):

 

[PB Client]

inv_RestClient.SendGetRequest("http://localhost:5000/api/Sample/F_get_picture/70", ls_data)

ljp_data = Create JSONPackage

ljp_data.Loadstring(ls_data)

ls_photo = ljp_data.GetValue("photo")

ljp_data.Loadstring(ls_photo)

ls_photo = ljp_data.GetValue("Value")


lnv_CoderObject = Create CoderObject

lb_pic = lnv_CoderObject.Base64Decode(ls_photo)

//Or

//lb_pic = blob (ls_photo)



p_1.setpicture(lb_pic)

 

Method #2: Parse once with the new feature provided by JasonParser in PowerBuilder 2019 R3 to get the value of the “Value” Key:

 

[PB Client]

inv_RestClient.SendGetRequest("http://localhost:5000/api/Sample/F_get_picture/70", ls_data)

//ljp_data = Create JSONPackage

//ljp_data.Loadstring(ls_data)

//ls_photo = ljp_data.GetValue("photo")

JsonParser lnv_JsonParser

String  ls_Path

lnv_JsonParser = Create JsonParser

lnv_JsonParser.LoadString(ls_data)

ls_Path = "/photo/Value"

ls_photo = lnv_JsonParser.GetItemString(ls_Path)

lnv_CoderObject = Create CoderObject

lb_pic = lnv_CoderObject.Base64Decode(ls_photo)

//Or

//lb_pic = blob (ls_photo)



p_1.setpicture(lb_pic)

 

           

 

Regards,

David

Comment
  1. Alex Riofrio
  2. Friday, 5 February 2021 15:04 PM UTC
Thanks a lot, David. Both options work very good :)

Finally I tried sending just the PbBlob object from the controller and it works perfectly as well.

In PB Client the code would look like this:



inv_RestClient.SendGetRequest("http://localhost:5000/api/Sample/f_get_picture/70", ls_data)



ljp_data = Create JSONPackage

ljp_data.LoadString(ls_data)

ls_photo = ljp_data.GetValue("Value")



lnv_CoderObject = Create CoderObject

lb_pic = lnv_CoderObject.Base64Decode(ls_photo)



p_1.setpicture(lb_pic)



Alex
  1. Helpful
There are no comments made yet.
Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Thursday, 4 February 2021 22:13 PM UTC
  2. SnapObjects
  3. # 1

Hi Alex,

PbBlob data type is part of the .NET DataStore libraries (specifically in the PowerScript.Bridge) library, which we added instead of using C# Byte data type so that PowerScript can be directly migrated to C#. 

There are differences between C# Byte data type and how PowerScript handles blob, for example in PB you can do things unbounded.  Here is documentation explaining the various features of PbBlob: https://docs.appeon.com/powerscript_bridge/1.0.3/api_reference/PowerScript.Bridge/PbBlob/PbBlob.html

If you are not migrating PowerScript to C#, you can consider to use Byte data type instead.  The .NET DataStore object itself doesn't require to use PbBlob.

If you want to use PbBlob and your code is not working, I recommend posting your code here so we can look what mistake you made and advise you.

Best regards,
Armeen

Comment
  1. Alex Riofrio
  2. Thursday, 4 February 2021 22:44 PM UTC
Thanks Armeen for your answer. Below I copy my code.



[SERVICE]

public PbBlob f_get_picture(short? ai_id)

{

var sql = @"SELECT LargePhoto FROM Production.ProductPhoto " +

"WHERE Production.ProductPhoto.ProductPhotoID = @ai_id";



var fotobyte = _context.SqlExecutor.Scalar<byte[]>(sql, ai_id);



return PbBlob.Create(fotobyte);

}



[CONTROLLER]

[HttpGet("{ai_id}")]

public ActionResult<IDataPacker> F_get_picture(short? ai_id)

{

DataPacker packer = new DataPacker();



var lbl_data = _service.f_get_picture(ai_id);

String ls_data = lbl_data.ToString();

packer.AddValue("photo", lbl_data);



return packer;

}



[PB Client]

inv_RestClient.SendGetRequest("http://localhost:5000/api/Sample/F_get_picture/70", ls_data)

ljp_data = Create JSONPackage

ljp_data.Loadstring(ls_data)

ls_photo = ljp_data.GetValue("photo")



lnv_CoderObject = Create CoderObject

lb_pic = lnv_CoderObject.Base64Decode(ls_photo)

//Or

//lb_pic = blob (ls_photo)



p_1.setpicture(lb_pic)
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Thursday, 4 February 2021 19:51 PM UTC
  2. SnapObjects
  3. # 2

Hi Alex;

  FWIW: The Blob type in PB is just a Byte[] array in C / C++ or C#. So stick with the Byte data type on the Web Service C# side.

Regards ... Chris

Comment
  1. Alex Riofrio
  2. Thursday, 4 February 2021 20:10 PM UTC
Thanks Chris for your answer.

Yes indeed, with Byte[] it works fine, I'm just exploring the PowerScript.Bridge library to see if I can use PbBlob in these cases.
  1. Helpful
  1. Kevin Ridley
  2. Thursday, 4 February 2021 21:07 PM UTC
What you pass "across the wire" needs to be a standard data type. If it's internal to your component, you could use PBBlob, but not if you are sending with a standard protocol (as far as I know).
  1. Helpful
  1. Armeen Mazda @Appeon
  2. Thursday, 4 February 2021 21:53 PM UTC
Byte in C# is not the same as Blob in PB, otherwise we would have not created PBBlob in C# and instead just directly mapped to Byte.
  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.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.