Read image file by webservice in AL

txerifftxeriff Member Posts: 340
edited 2020-09-18 in NAV Three Tier
Hi All,

I hope somebody can help.

Im trying to read an image returned from a webservice, however, I´m not sure much about it.
This is what I´ve used for reading xml response which is fine, since is a text that I then parse, but as image, I´ve never done before.
Any ideas?
thanks a lot.

RequestHeaders := Client.DefaultRequestHeaders();
RequestHeaders.Add('Authorization', CreateBasicAuthHeader(Username, Password));
Client.Get(RequestURL, ResponseMessage);

ResponseMessage.Content().ReadAs(ResponseText);

//TempBlob.ReadAsText(ResponseText, TextEncoding::UTF8);
//TempBlob.Blob.CreateOutStream(vOutStr);
vTempPath := FileMgt.ClientTempFileName('.jpg');
vFile.Create(vTempPath);
vFile.CreateOutStream(vOutStr);
vOutStr.Write(ResponseText);

vFile.Close();



EDIT:

Sorry, I´ve solved myself so I will paste in here for any1 else:


You can read the content into a instream and then, read byte by byte (vchars is a byte type, otherwise it won´t work)

Client: HttpClient;
RequestHeaders: HttpHeaders;
RequestContent: HttpContent;
ResponseMessage: HttpResponseMessage;
RequestMessage: HttpRequestMessage;
ResponseText: Text;
contentHeaders: HttpHeaders;
TempBlob: Record TempBlob temporary;
vOutStr: OutStream;
vInstr: InStream;
vFile: File;
vTempPath: Text;
FileMgt: Codeunit "File Management";
vChars: Byte;


ResponseMessage.Content().ReadAs(vinStr);


vTempPath := FileMgt.ClientTempFileName('.jpg');

vFile.Create(vTempPath);
vFile.CreateOutStream(vOutStr);

repeat
vInstr.Read(vChars, 1);
vOutStr.Write(vChars, 1);

until vInstr.EOS;

vFile.Close();
Sign In or Register to comment.