top of page

Download Binary Files With BC

Binary files might seem scary, right? Let's calm your fears ;-)

When you type in a URL in your web browser, your web browser usually makes an HTTP GET request.


So let's do it in BC by following an example to download a simple PDF file!


This is our PDF file:


We throw a HTTP GET Request by using our wrapper function. Did you read our blog using our wrapper functions?


This example is available on our github project:


codeunit 50002 "BYD Examples Mgt."
{
    procedure DownloadPDFfromURL()
    var
        Instr: InStream;
        Filename: Text;
    begin
        Filename := 'BEYONDCloudConnector.pdf';
        BYDWebRequestMgt.ResponseAsInStr(Instr, BYDWebRequestMgt.PerformWebRequest('https://en.beyond-cloudconnector.de/_files/ugd/96eaf6_16d4b7b24ce249339594fb661dbb7f48.pdf', Enum::"BYD Web Request Method"::GET));
        DownloadFromStream(Instr, SaveFileDialogTitleMsg, '', SaveFileDialogFilterMsg, Filename);
    end;

    var
        BYDWebRequestMgt: Codeunit "BYD Web Request Mgt.";
        SaveFileDialogFilterMsg: Label 'PDF Files (*.pdf)|*.pdf';
        SaveFileDialogTitleMsg: Label 'Save PDF file';
}

We are performing the Request here:

procedure PerformWebRequest(Url: Text; Method: Enum "BYD Web Request Method"; RequestHeaders: Dictionary of [Text, Text]; var Content: HttpContent): HttpResponseMessage;
var
	Client: HttpClient;
	Response: HttpResponseMessage;
	HeaderKey: Text;
	HeaderValue: Text;
begin
	foreach HeaderKey in RequestHeaders.Keys() do begin
		RequestHeaders.Get(HeaderKey, HeaderValue);
		Client.DefaultRequestHeaders.Add(HeaderKey, HeaderValue);
	end;

	case Method of
		Method::GET:
			Client.Get(Url, Response);
		Method::POST:
			Client.Post(Url, Content, Response);
		Method::PUT:
			Client.Put(Url, Content, Response);
		Method::DELETE:
			Client.Delete(Url, Response);
	end;
	exit(Response);
end;

And return the following response as InStream:

procedure ResponseAsInStr(var InStr: InStream; Response: HttpResponseMessage)
begin
	Response.Content.ReadAs(InStr);
end;

See how it works:




641 Ansichten0 Kommentare

Aktuelle Beiträge

Alle ansehen

BeyondCues

Make your employees' everyday life easier and increase productivity at the same time. BEYOND Cues adds a tab to your role center with “Cues”. Among other things, the cues can be configured to display

bottom of page