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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Simple AX / Again on FTP trasmission (d...

Again on FTP trasmission (dotNET mode)

Daniele Ferraretto Profile Picture Daniele Ferraretto

Here below a private methods of an ad-hoc class for trasmit data.
In other sources you’ll find the trasmission using a stream reader with string codification (UTF, ASCII and so on). The below adapt the procedure using a binary stream (example for trasmit PDF file or ZIP).
Consider very important to cle the binary stream in reading if, after the trasmission, you need to manage the original source (example : move file in a folder backup).

</pre>
private void FTPCommandDotNet(str _localPath, str _filename)
{
object ftpo;
object ftpResponse;
System.Net.FtpWebRequest request;
System.IO.StreamReader reader;
System.IO.Stream requestStream;
System.Byte[] bytes, bytesReadChk;
System.Net.NetworkCredential credential;
System.String xmlContent;
System.Text.Encoding utf8;
int i, chk;
System.IO.BinaryReader readbin;
System.IO.FileStream streamBin;
System.Net.FtpWebResponse response;
;

streamBin = System.IO.File::Open(strfmt("%1%2",_localPath,_filename), System.IO.FileMode::Open);
readbin = new System.IO.BinaryReader(streamBin);
i = streamBin.get_Length();
bytes = readbin.ReadBytes(i);

//bytes = utf8.GetBytes( );
//reader.Close();

i = bytes.get_Length();

ftpo = System.Net.WebRequest::Create(strfmt(@"ftp://%1/%2/%3", ftpServer, destinationFolder, _filename));
request = ftpo;

credential = new System.Net.NetworkCredential(user,password);
request.set_Credentials(credential);
request.set_ContentLength(bytes.get_Length());
request.set_Method("STOR");
request.set_UseBinary(true);

//writing
requestStream = request.GetRequestStream();
requestStream.Write(bytes,0,bytes.get_Length());
requestStream.Close();

/*
//TODO check reading
request.set_Method(System.Net.WebRequestMethods);
request.set_UseBinary(true);

requestStream = request.GetRequestStream();
chk = request.get_ContentLength();
requestStream.Read(bytesReadChk,0, chk);
requestStream.Close();
chk = bytesReadChk.get_Length();

if(i!= chk)
warning("Difference between write and read");
*/
ftpResponse = request.GetResponse();
response = ftpResponse;
//info(response.get_StatusDescription());
readbin.Close();
readbin = null;
}
<pre>


This was originally posted here.

Comments

*This post is locked for comments