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 :
Finance | Project Operations, Human Resources, ...
Answered

Save text file as ANSI on FTP server

(0) ShareShare
ReportReport
Posted on by 555
Hi experts,
 
I have the code below, that can make an utf-8 file on a FTP server with username and password. That works fine.
 
The only problem is that I need to save the file as ANSI, so the Danish letters are correct.
 
In the Axapta 2009 program, I make an ANSI file in the Windows temp folder. That file is correct, but when I use the code below to send it to the FTP server, the Danish letters get wrong.
 
I hope you can help!
 
Best regards,
 
Morten
 
public void uploadFiletoFTPServer(str _fileNameTmp, str _fileNameFtp)
{
    System.Object request,response,credential;
    System.IO.StreamReader streamReader;
    System.IO.Stream requestStream;
    System.Array files;
    System.Net.FtpWebRequest ftpRequest;
    System.Net.FtpWebResponse ftpResponse;
    System.Byte[] bytes;
    System.Text.Encoding myEncoding;
    System.Exception sysException;
    Str 120 TempPath,executedate;
    str timeinstr,nextFile;
    Commaio file;
    container line;
    Filename filepath,fileType, ftpFileName;
    System.Net.ICredentials iCredentials;
    System.Net.IWebProxy    iWebProxy;
    ;
    try
    {
        ftpFileName = /ftp...//myFile/;
        streamReader = new System.IO.StreamReader(_fileNameTmp);
        // must be ANSI:
        myEncoding = System.Text.Encoding::get_UTF8();
        //myEncoding = System.Text.Encoding::get_Default();
        //myEncoding = System.Text.Encoding::get_Unicode();
        //myEncoding = System.Text.Encoding::GetEncoding(1252);
        //myEncoding = System.Text.Encoding::get_ASCII();
        //myEncoding = System.Text.Encoding::get_BigEndianUnicode();
        //myEncoding = System.Text.Encoding::GetEncoding(865);
        //myEncoding = System.Text.Encoding::GetEncoding(65001);
        bytes = myEncoding.GetBytes(streamReader.ReadToEnd());
        
        streamReader.Close();
        request = System.Net.WebRequest::Create(new System.Uri(ftpFileName));
        ftpRequest = request;
        credential = new System.Net.NetworkCredential(/myUserId/, /myPassword/);
        iCredentials = credential;
        ftpRequest.set_Credentials(iCredentials);
        ftpRequest.set_ContentLength(bytes.get_Length());
        ftpRequest.set_Method(/STOR/);
        requestStream = ftpRequest.GetRequestStream();
        requestStream.Write(bytes,0,bytes.get_Length());
        requestStream.Close();
        response = ftpRequest.GetResponse();
        ftpResponse = response;
    }
    catch(Exception::CLRError)
    {
        sysException = CLRInterop::getLastException();
        info(sysException.get_Message());
    }
    CodeAccessPermission::revertAssert();
}
I have the same question (0)
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,390 Super User 2025 Season 2 on at
    Hi @Morten,
     
    Try to use this encoding iso-8859-1
    myEncoding = System.Text.Encoding::GetEncoding(28591);
    Best regards,
    Mohamed Amine MAHMOUDI
  • Morten Steengaard Profile Picture
    555 on at
    Hi Mohamed,
    Thank you. Sadly that didn't help. The Danish letters are shown as question marks in the file on the FTP server.
    Best regards,
    Morten
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,390 Super User 2025 Season 2 on at
    Hi @Morten,
     
    I think the issue is due the StreamReader.
    try this 
    streamReader = new System.IO.StreamReader(_fileNameTmp, System.Text.Encoding::UTF8);
    Best regards,
    Mohamed Amine MAHMOUDI
  • Morten Steengaard Profile Picture
    555 on at
    Hi Mohamed,
     
    If I changed your suggestion to this:
     
    streamReader = new System.IO.StreamReader(_fileNameTmp, System.Text.Encoding::GetEncoding(865);
     
    and this:
     
    myEncoding = System.Text.Encoding::GetEncoding(865);
     
    then it works!
     
    Thank you very much!
     
    Best regards,
     
    Morten
     
  • Mohamed Amine Mahmoudi Profile Picture
    26,390 Super User 2025 Season 2 on at
    Hi @Morten,
     
    if everything is ok please mark the answer as verified
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • Verified answer
    Morten Steengaard Profile Picture
    555 on at
    Hi Mohamed,
     
    I cannot mark your answer as verified. When I use Edge or Chrome (normal or incognito) and press "Does this answer your question?", then I only get a turning circle that goes on and on for hours.
     
    So I cannot give you credit for your good help.
     
    I'll try to mark my own text as the answer, so that future readers at least can see that my problem has been solved. Then they'll find your answer.
     
    Best regards,
     
    Morten

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans