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 :
Microsoft Dynamics AX (Archived)

FTP upload with System.Net.FtpWebRequest uploads corrupt files.

(0) ShareShare
ReportReport
Posted on by 4,075

I have tried to upload a text file to my ftp server using this script, based on this article : http://microsoftdax.blogspot.dk/2015/05/ax-2012-upload-file-to-ftp-with-batch.html

But even though the file gets uploaded, it is corrupt, usually with a file that is way to big.

It seems to have something to do with the encoding of the file, but i can't figure out how to get it to work, any ideas?

private server static void sendToFTP()
{
    FilePath    localFilePath   = @'';
    FilePath    remoteFilePath  = @'';
    str         searchPattern   = '';
    str         ftpAddress      = '';
    str         ftpPath         = '';
    str         ftpUser         = '';
    str         ftpPass         = '';

    System.Object                   ftpo;
    System.Object                   ftpResponse;
    System.Net.FtpWebRequest        request;
    System.IO.StreamReader          reader;
    System.IO.Stream                requestStream;
    System.Byte[]                   bytes;

    System.Text.Encoding            utf8;
    System.Net.FtpWebResponse       response;
    System.Net.NetworkCredential    credential;

    int             fileCount;
    System.Array    files;
    int             i;
    str             nextFile;
    Filename        filepath;
    Filename        fileType,fileNameString;
    FileNameType    pattern = '*.TXT';
    FileName        ftpFileName,fileName;
    str             uriAux;


    new InteropPermission(InteropKind::ClrInterop).assert();

    files           = System.IO.Directory::GetFiles(localFilePath, searchPattern);

    if (files)
    {
        fileCount =    files.get_Length();

        for(i=0; i < fileCount; i++)
        {
            nextFile    = files.GetValue(i);

            [filepath, filename, fileType] = fileNameSplit(nextFile);
            fileNameString= filename + fileType;
            ftpFileName = 'ftp://' + ftpAddress + '/' + ftpPath + '/' + fileNameString;

            reader = new System.IO.StreamReader(nextFile);
            utf8 = System.Text.Encoding::get_UTF8();
            bytes = utf8.GetBytes( reader.ReadToEnd() );
            reader.Close();
            // little workaround to get around the casting in .NET
            ftpo = System.Net.WebRequest::Create(ftpFileName);
            request = ftpo;

            credential = new System.Net.NetworkCredential(ftpUser,ftpPass);
            request.set_Credentials(credential);

            request.set_ContentLength(bytes.get_Length());
            request.set_Method('STOR');
            // "Bypass" a HTTP Proxy (FTP transfer through a proxy causes an exception)
            // request.set_Proxy( System.Net.GlobalProxySelection::GetEmptyWebProxy() );
            requestStream = request.GetRequestStream();
            requestStream.Write(bytes,0,bytes.get_Length());
            requestStream.Close();

            ftpResponse = request.GetResponse();
            response = ftpResponse;
            info(response.get_StatusDescription());
        }
    }

    CodeAccessPermission::revertAssert();
}


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    nunomaia Profile Picture
    25 Moderator on at

    Instead of forcing the text encoding, try to upload the file as binary. 

    bytes = System.IO.File::ReadAllBytes(nextFile);


  • Rudi Hansen Profile Picture
    4,075 on at

    Ohh that seemed to work, will do some more testing tomorrow, but it seems that solved it.

    Thanks

    Edit : Yes problem solved, now binary and text files are uploaded correct, thanks for the help.

  • Josan_ec Profile Picture
    45 on at

    Hi,

    I have a problem in this line:

    "requestStream = request.GetRequestStream();"

    What's going on?
  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Josan_ec, you seem to have a different problem than what was discussed in this (already answered) thread. Therefore please don't try to change the topic here; create a new thread instead and explain your problem in detail there.

  • babalalex Profile Picture
    411 on at

    Hey Jason, if you got any headway for this.

    Can you please reply on this link ?

    community.dynamics.com/.../ftp-upload-with-system-net-ftpwebrequest---error-550

    I dont want to extend this thread unnecessarily.

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans