Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Save text file as ANSI on FTP server

(1) ShareShare
ReportReport
Posted on by 543
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();
}
  • Verified answer
    Morten Steengaard Profile Picture
    Morten Steengaard 543 on at
    Save text file as ANSI on FTP server
    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
  • Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 10,130 Super User 2024 Season 2 on at
    Save text file as ANSI on FTP server
    Hi @Morten,
     
    if everything is ok please mark the answer as verified
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • Morten Steengaard Profile Picture
    Morten Steengaard 543 on at
    Save text file as ANSI on FTP server
    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
     
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 10,130 Super User 2024 Season 2 on at
    Save text file as ANSI on FTP server
    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
    Morten Steengaard 543 on at
    Save text file as ANSI on FTP server
    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
    Mohamed Amine Mahmoudi 10,130 Super User 2024 Season 2 on at
    Save text file as ANSI on FTP server
    Hi @Morten,
     
    Try to use this encoding iso-8859-1
    myEncoding = System.Text.Encoding::GetEncoding(28591);
    Best regards,
    Mohamed Amine MAHMOUDI

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,602 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,340 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans