Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Business Central forum

Import files AL - Visual Studio Code - Business Central

(0) ShareShare
ReportReport
Posted on by

Hello everyone,

I'm developing as extension in Business Central (SaaS - so no File Management codeunit or internal C/AL code available).

The customer needs a procedure which reads a file they have hosted in their machines and changes some values in the Item Card based on what the file contains.

Since the file comes from a 3rd party software, the format can't be changed.

As I can't use any internal functions (such as File Management Codeunit or XML Ports with upload / download features), how can I write a function which simply allows the user to select a file from their PC and gives me the content as InStream / Txt o others?

Many thanks for your help,

Michael

Categories:
  • Verified answer
    Hannes Holst Profile Picture
    5,767 on at
    RE: Import files AL - Visual Studio Code - Business Central

    hi,

    Open existing File Attachment:

        procedure OpenAttachment(pFileAttachmentEntryNo: Integer)
        var
            AttachmentRec: record Attachment;
            ResponseStream: InStream;
            tempfilename: text;
    ErrorAttachment: Label 'No file available.'; begin if AttachmentRec.get(pFileAttachmentEntryNo) then if AttachmentRec."Attachment File".HasValue then begin AttachmentRec.CalcFields("Attachment File"); AttachmentRec."Attachment File".CreateInStream(ResponseStream); tempfilename := CreateGuid() + '.' + AttachmentRec."File Extension"; DOWNLOADFROMSTREAM(ResponseStream, 'Export', '', 'All Files (*.*)|*.*', tempfilename); end else Error(ErrorAttachment); end;


    Upload new attachment:

        procedure UploadAttachment()
        var
            AttachmentRec: Record Attachment;
            FileOutStream: OutStream;
            FileInStream: InStream;
            tempfilename: text;
            DialogTitle: Label 'Please select a File...';
        begin
            if UploadIntoStream(DialogTitle, '', 'All Files (*.*)|*.*', tempfilename, FileInStream) then begin
    
                AttachmentRec.Init();
                AttachmentRec.Insert(true);
                AttachmentRec."Storage Type" := AttachmentRec."Storage Type"::Embedded;
                AttachmentRec."Storage Pointer" := '';
                AttachmentRec."File Extension" := GetFileType(tempfilename);
                AttachmentRec."Attachment File".CreateOutStream(FileOutStream);
                CopyStream(FileOutStream, FileInStream);
                AttachmentRec.Modify(true);
    
            end;
        end;


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central forum

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans