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)