How to return the actual content of a TBlob

Hi,


Im almost done testing XData, everything good so far except one thing. As the question refers and as your docs says (http://www.tmssoftware.biz/business/xdata/doc/web/index.html?blob_representation.htm), when i try to return a class with a TBlob field on it, i get the "path" representation of it ("Client(1)/Photo"). The problem is that, this class is not a persistent class, it is only mapped as a custom class as you can see bellow.

[Entity]
[Id('Id', TIdGenerator.None)]
TClient = class
public
  [Column]
  Id : Integer;

  [Column]
  Photo : TBlob;
end;

So, my question is, how can i actually get the content of this blob and not the proxy "path"?

There is not a direct configuration to do that, but there are workarounds.

I suppose you are returning this from an action? In this case you have several options:

1. Return a TStream instead of TClient. Then you can return the blob content in the stream. If you still need to return the Id, you can use TJsonWriter to write a JSON response to the stream.

2. If the object doesn't have an ID, blob will be serialised inline. So you can just set Id property to 0, or create another fake id and let it be 0.

If you say the class is transient, I'm not sure why you want to use the Id property of it? Seems like you just want to return the blob content? If that's the case option 1 is your best choice.

Well indeed the class is transient, and it is mapped as i posted before. I tried to remove the [Id] attribute from my class declaration since it is not persistent, but after i remove i get a error saying:


EMappingNotFound: 'Cannot find mapping [id] on class TClient'.

Anyway reading ur docs somewhere it says that the content of the blob is encrypted in base64, with that in mind i made a different approach as described bellow:

//Indy 10
uses IdCoderMIME;

[Entity]
[Id('Id', TIdGenerator.None)]
TClient = class
public
  [Column]
  Id : Integer;

  [Column]
  Photo : string; //change it instead of TBlob
end;

and did this in my action result

function TMyService.MyAction() : TClient;
begin
  //other things ...

  //MyStream: holds my blob content
  //TIdEncoderMIME.EncodeStream: Indy function to encode stream content as base64
  Result.Photo := TIdEncoderMIME.EncodeStream(MyStream);
end;

well, this solved the problem, with this response content i could re-create the file in delphi and in php.

Glad you solved the problem. But as I said if you don't need the id, you could have just set it to zero and blob will be serialized inline.