web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

TextEncoding while using UploadintoStream function

(1) ShareShare
ReportReport
Posted on by 414

Hi,

I am using UploadIntoStream function to choose a file and then get its data in the stream. The file has Danish characters that not work fine after they are read and special Danish characters change their format. 

Is there a way to use TextEncoding with this UploadintoStream function.

I have the same question (0)
  • Suggested answer
    srttel Profile Picture
    on at

    Hi Bilal,

    please have a look at this github entry where something very similar was discussed.

    github.com/.../2538

    there is an example how to use the text encoding.

  • Bilal Haider Profile Picture
    414 on at

    Thanks but issue still remains unsolved.

    Even with using TempBlob function BlobImportWithFilter and explicitly giving Text Encoding still I am facing the same issues.

  • Suggested answer
    Suresh Kulla Profile Picture
    50,247 Super User 2025 Season 2 on at

    please check the file management codeunit it has some functions to apply encoding to the streams.

  • Suggested answer
    Bilal Haider Profile Picture
    414 on at

    Hi Suresh,

    Thanks for you reply. Actually on SaaS version the TextEncoding::Windows does not work. I have tried with BlobImportWithFilter functions. I have temporary solved this issue by saving the text file in UTF-8 format.

    If you have any suggestions please

  • Suggested answer
    Maxime Caty Profile Picture
    6 on at
    It seem like the "UploadIntoStream" function alway create a new InStream instance with windows encoding (which is weird as BC internaly use UTF8 for text).
     
    You can fix this afterwhat if you copy it into an OutStream, then back again to an InStream but this time with specific TextEncoding.
     
    var
        InStr: InStream;
        OutStr: OutStream;
        FormFile: Text;
        ReadLine: Text;
        ReadText: Text;
        Blob: Codeunit "Temp Blob";
    begin
        UploadIntoStream('Choose a file', '', '', FormFile, InStr);

        // Show original InStream as Text
        ReadLine := '';
        while (InStr.ReadText(ReadLine) > 0) do
            ReadText += ReadLine;
        Message(ReadText);

        // utf8 transcode
        Blob.CreateOutStream(OutStr);
        InStr.ResetPosition();
        CopyStream(OutStr, InStr);
        Blob.CreateInStream(InStr, TextEncoding::UTF8);

        // Show new InStream as Text​​​​​​​
        ReadLine := '';
        ReadText := '';
        while (InStr.ReadText(ReadLine) > 0) do
            ReadText += ReadLine;
        Message(ReadText);
    end;
     
    Here result with an utf8 xml file :
     
    Original :
     
     
    After transcoding :
     
     
  • Suggested answer
    RWCH Profile Picture
    2 on at

    I notice a few things looking at the answers. A temporary blob is used, which is not necesssary and a text variable is filled with the content of the file, which also is not necessary.  I think it better to just use streams. What goes is, goes in. No need to worry about the encoding. Same thing, when getting it out. This is what it could/should look like:

        actions
        {
            area(processing)
            {
                action(UploadBlob)
                {
                    Caption = 'Upload file';
                    Promoted = true;
                    PromotedCategory = Process;

                    trigger OnAction()
                    begin
                        uploadFile();
                    end;
                }
            }
        }


        local procedure uploadFile()
        var
            InStr: InStream;
            FormFile: Text;
        begin
            if not UploadIntoStream('Choose a file', '', '', FormFile, InStr) then begin
                Message('File upload canceled.');
                exit;
            end;

            processInstream(InStr);
        end;

        local procedure processInstream(InStr: InStream)
        var
            OutStr: OutStream;
        begin
            Rec.Data.CreateOutStream(OutStr);
            CopyStream(OutStr, InStr);
            Rec.Modify();
            Commit();
            //CurrPage.Update(true);
            Rec.Data.Export('C:\Temp\' + Rec.FileName);
        end;

    I don't think the line CurrPage.Update(true); is necessary. I don't understand what it exactly does.

    Disclaimer: I am an experienced developer, but fairly new to developing with AL in Business Central. 


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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,116

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 764 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 635 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans