Download binary files with Business Central

Download Binary Files With BC

Daniel Gorski
Daniel Gorski 08. März 2022
2 Min. Lesezeit

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:

👉 BEYOND Cloud Connector

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

👉 Use Code to Clean Code – Wrapper Funktionen

Code is available on GitHub:
👉 https://github.com/byndit/BeyondAL


💻 AL Code: codeunit 50002 "BYD Examples Mgt."

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';
}

🔁 Wrapper: Perform Web Request

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;

🔄 Convert Response to InStream

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

🎥 See how it works

Demonstration

beyondit
tech
Business Central
HTTP
Download
Binary Files
Azure Communication Services SMTP with Scanner

Using Azure Communication Services SMTP with Your Scanner or Printer

Need to send scans via email from your office scanner? Azure Communication Services provides a reliable SMTP relay that works with any device supporting SMTP au

Weiter lesen
Weiter lesen
XRechnung Einrichtung in Business Central

XRechnung in Business Central einrichten: Schritt für Schritt zur E-Rechnung

Ab 2025 wird die E-Rechnung im B2B-Bereich in Deutschland Pflicht. Wir zeigen Ihnen, wie Sie XRechnung in Business Central einrichten – mit E-Beleg-Dienst, Work

Weiter lesen
Weiter lesen