Zum Inhalt springen
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
E-Invoicing-Formate UBL, CII, ZUGFeRD und Factur-X im Vergleich

UBL, CII, ZUGFeRD, Factur-X: Die E-Invoicing-Formate endlich verständlich erklärt

UBL, CII, ZUGFeRD oder Factur-X – diese E-Invoicing-Formate werden ständig verwechselt. Wir erklären den Unterschied zwischen reinen XML-Formaten und Hybridform

Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: Die E-Invoicing-Formate endlich verständlich erklärt
Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: Die E-Invoicing-Formate endlich verständlich erklärt
E-Invoicing formats UBL, CII, ZUGFeRD and Factur-X compared

UBL, CII, ZUGFeRD, Factur-X: E-Invoicing Formats Finally Explained

UBL, CII, ZUGFeRD or Factur-X – these e-invoicing formats get confused all the time. We break down the difference between pure XML formats and hybrid PDF-plus-X

Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: E-Invoicing Formats Finally Explained
Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: E-Invoicing Formats Finally Explained