-->

Dynamics AX FTP

2019-07-18 05:18发布

问题:

I have problems to send csv file on ftp, I use the following code found on AxaptaPedia:

object ftpo;
object ftpResponse;
System.Net.FtpWebRequest request;
System.IO.StreamReader reader;
System.IO.Stream requestStream;
System.Byte[] bytes;
System.Net.NetworkCredential credential;
System.String xmlContent;
System.Text.Encoding utf8;

System.Net.FtpWebResponse response;
;
reader = new System.IO.StreamReader(strfmt("%1%2","\\\\Server\\directory\\","Export.csv"));
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes( reader.ReadToEnd() );
reader.Close();

ftpo = System.Net.WebRequest::Create(strfmt("%1%2","ftp://IP_Address/directory/","Export.csv"));
request = ftpo;

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

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

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

No compilation errors, no execution errors but file is not uploaded on my FTP, I think the problem is on the path file coding, i tried other solutions without results. I want to process send file on FTP in batch, it works using wininet class but wininet class can't be run on server side so I must use .net framework. Any ideas or solution is welcome

thanks for help

回答1:

I have had this issue with saving files to any network directory and not just FTP. What worked for me was to create separate variables for both the server and the file name and use concatenation instead of string format (StrFmt). for some reason the string formatting just silently fails to work, while concatenation does work:

str  server = "\\\\Server\\directory\\";
str  fileName = "Export.csv";
Net.WebRequest::Create(server + fileName);

Also best practice would not be to hard code either of these values. you should either create a parameter table to store these values and make a call to the table to find the values and assign them to your variables or use a dialog to get the input from the user at run-time. This avoids anyone in the future from having to change the code if the FTP server changes and also avoids issues where the file name is changed as well.