Dynamics AX 2012 – Downloading a file from FTP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dynamics AX 2012 – Downloading a file from FTP相关的知识,希望对你有一定的参考价值。

Back in Feb, was trying to download a file from FTP server by the conventional way using the native X++ code, though we have several approaches and free tools to achieve the same. When i did some research, couldn’t able to find any references in X++ which meets the objective but with few references on FTPWebRequest and FTPWebResponse APIs.

Using the commands from the above APIs, had successfully established connection to the FTP server by passing, authorizing and authenticating valid credentials. Once established the connection, were able to download all the files from the specified path and then deleted them once all the files got downloaded.

The following piece explains the definition and declaration of variables:

// CurrentList inputs declaration
Freq ftpTimeOut;
Port ftpPortNum;
Password ftpPassword;
UserName ftpUserName;
URL ftpHostName;
FilePath saveToFilePath;
 
BK_FTPDownloadSetup ftpDownloadSetup;
 
// FTP files list
List ftpFilesList;
ListEnumerator ftpFilesListEnum;
 
// Dialog fields declaration
DialogField dialogTimeOut;
DialogField dialogPortNum;
DialogField dialogPassword;
DialogField dialogUserName;
DialogField dialogHostName;
DialogField dialogSaveToFilePath;
 
// Dialog object declaration
DialogRunBase dialogRunBase;
 
// FTP objects declaration
Object ftpObject;
Object ftpResponse;
 
System.String strReadLine;
 
System.IO.Stream iostream;
System.IO.StreamReader ioStreamReader;
System.IO.StreamWriter ioStreamWriter;
 
System.Net.FtpWebRequest ftpWebRequest;
System.Net.FtpWebResponse ftpWebResponse;
System.Net.NetworkCredential networkCredentials;
 
// Macro - FTP public fields
#define.DeleteFile("DELE")
#define.DownloadFile("RETR")
#define.ListDirectory("NLST")
 
#define.ClrFileAccessEnum (‘System.IO.FileAccess‘)
#define.ClrFileAccessWrite (‘Write‘)
 
// Macro
#define.CurrentVersion(2)
#LOCALMACRO.CurrentList
ftpTimeOut,
ftpPortNum,
ftpPassword,
ftpUserName,
ftpHostName,
saveToFilePath
#ENDMACRO

 The following code helps to explore the files in the given directory.

private void getFTPDirFilesList()
{
container conFTPFilesDownload;
ListIterator ftpFilesListIterator;
 
// Marshaling .NET to X++
ftpObject = System.Net.WebRequest::Create(ftpHostName);
ftpWebRequest = ftpObject;
 
if (ftpWebRequest)
{
ftpWebRequest.set_KeepAlive(false);
ftpWebRequest.set_UsePassive(true);
ftpWebRequest.set_UseBinary(true);
ftpWebRequest.set_Timeout(ftpTimeOut);
ftpWebRequest.set_Method(#ListDirectory);
this.setFTPCredentials();
 
ftpWebResponse = ftpWebRequest.GetResponse();
 
if (ftpWebResponse)
{
ftpFilesList = new list(Types::String);
 
// BP Deviation Documented
ioStreamReader = new System.IO.StreamReader(ftpWebResponse.GetResponseStream());
 
if (ioStreamReader)
{
strReadLine = ioStreamReader.ReadLine();
 
while (!System.String::IsNullOrEmpty(strReadLine))
{
ftpFilesListIterator = new Listiterator(strsplit(strReadLine, ‘/‘));
 
while (ftpFilesListIterator.more())
{
conFTPFilesDownload += ftpFilesListIterator.value();
ftpFilesListIterator.next();
}
 
ftpFilesList.addEnd(conPeek(conFTPFilesDownload, conlen(conFTPFilesDownload)));
strReadLine = ioStreamReader.ReadLine();
}
 
ioStreamReader.Close();
}
 
if (ftpFilesList.empty())
{
warning (strfmt("No files available in %1", ftpHostName));
}
}
}
}

  The code below is used to set the credentials

private void setFTPCredentials()
{
 
// BP Deviation Documented
networkCredentials = new System.Net.NetworkCredential(ftpUserName, ftpPassword);
 
ftpWebRequest.set_Credentials(networkCredentials);
}

  The following code is actually doing the file download

private void ftpDownloadAllFiles()
{
str fileNameType;
 
FilePath filePathDest;
;
 
ftpFilesListEnum = ftpFilesList.getEnumerator();
ftpFilesListEnum.reset();
 
while (ftpFilesListEnum.moveNext())
{
fileNameType = ftpFilesListEnum.current();
 
// Marshaling .NET to X++
ftpObject = System.Net.WebRequest::Create(ftpHostName + @"/" + fileNameType);
ftpWebRequest = ftpObject;
 
if (ftpWebRequest)
{
ftpWebRequest.set_KeepAlive(false);
ftpWebRequest.set_UsePassive(true);
ftpWebRequest.set_UseBinary(true);
ftpWebRequest.set_Timeout(ftpTimeOut);
ftpWebRequest.set_Method(#DownloadFile);
ftpWebRequest.set_ReadWriteTimeout(ftpTimeOut);
this.setFTPCredentials();
 
ftpWebResponse = ftpWebRequest.GetResponse();
 
// BP Deviation Documented
ioStreamReader = new System.IO.StreamReader(ftpWebResponse.GetResponseStream());
 
if (ioStreamReader)
{
strReadLine = ioStreamReader.ReadToEnd();
 
if (strReadLine)
{
filePathDest = saveToFilePath + @"\" + fileNameType;
this.writeFile(filePathDest);
 
info(strfmt("Downloaded file %1 to %2", fileNameType, saveToFilePath));
}
 
ioStreamReader.Close();
}
}
}
}

  The following code is used to write all the downloaded files

private void writeFile(FilePath _filePath)
{
 
// BP Deviation Documented
ioStreamWriter = new System.IO.StreamWriter(_filePath);
 
if (ioStreamWriter)
{
ioStreamWriter.Write(strReadLine);
ioStreamWriter.Flush();
ioStreamWriter.Close();
}
}

  The following code is used to delete the files to delete them all.

private void ftpDeleteAllFiles()
{
Str fileNameType;
 
ftpFilesListEnum = ftpFilesList.getEnumerator();
ftpFilesListEnum.reset();
 
while (ftpFilesListEnum.moveNext())
{
fileNameType = ftpFilesListEnum.current();
 
// Marshaling .NET to X++
ftpObject = System.Net.WebRequest::Create(ftpHostName + @"/" + fileNameType);
ftpWebRequest = ftpObject;
 
if (ftpWebRequest)
{
// ftpWebRequest.set_KeepAlive(false);
// ftpWebRequest.set_UsePassive(true);
// ftpWebRequest.set_UseBinary(true);
ftpWebRequest.set_Method(#DeleteFile);
// ftpWebRequest.set_Timeout(ftpTimeOut);
// ftpWebRequest.set_ReadWriteTimeout(ftpTimeOut);
this.setFTPCredentials();
 
ftpWebResponse = ftpWebRequest.GetResponse();
 
if (ftpWebResponse)
{
info(strfmt("Deleted file %1 from %2", fileNameType, ftpHostName));
}
}
}

  

原文地址:https://daxbalakumaran.wordpress.com/2015/12/27/dynamics-ax-2012-downloading-a-file-from-ftp/

 

以上是关于Dynamics AX 2012 – Downloading a file from FTP的主要内容,如果未能解决你的问题,请参考以下文章

Dynamics AX 2012 DLL 配置文件未更新

Dynamics AX 2012 – Downloading a file from FTP

AX 2012 现有量表加字段

dynamics ax 2012:将 RowState 从 Edit 更改为 Selected

Dynamics AX 2012 R2 窗体系列 - 在窗体上修改字段时所触发的方法及其顺序

在我的 wcf 服务中验证 AX 服务器域、用户名和密码等凭据后,下载 Microsoft Dynamics AX 2012 服务 wsdl 元数据