Skip to content
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
GoBD-konforme Archivierung mit Business Central und SharePoint ohne DMS

Architektur

Revisionssicher archivieren ohne DMS? Wie weit Business Central, SharePoint und Azure Blob Storage wirklich tragen

Warum „GoBD" oft vorschnell zum K.O.-Kriterium wird – und wie weit Business Central, SharePoint und Azure Blob Storage wirklich tragen.

Read More: Revisionssicher archivieren ohne DMS? Wie weit Business Central, SharePoint und Azure Blob Storage wirklich tragen
Read More: Revisionssicher archivieren ohne DMS? Wie weit Business Central, SharePoint und Azure Blob Storage wirklich tragen
Modulare ERP-Architektur für skalierbare Unternehmenssoftware

Architektur

Mehr als nur Warenwirtschaft - die Landtechnik Lösung

ERP-Systeme wurden jahrelang als Monolithen verkauft. Die eigentliche Stärke moderner Plattformen wie Business Central liegt jedoch in der Modularität - und gen

Read More: Mehr als nur Warenwirtschaft - die Landtechnik Lösung
Read More: Mehr als nur Warenwirtschaft - die Landtechnik Lösung