【转】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/

时间: 2024-12-29 23:32:23

【转】Dynamics AX 2012 – Downloading a file from FTP的相关文章

Dynamics AX 2012 – Batch Jobs Not Executing

In Dynamics AX 2012, there are times when batch jobs appear to be stranded in a waiting status.  When this happens, the cause can typically be a batch server or batch group which is not set up properly.   However, if these are set up correctly and yo

Dynamics AX 2012 R3 Demo 安装与配置 - 导入测试数据 (Step 4)

    在前面三节中,Reinhard分别讲解了如何配置安装环境,安装数据库服务器,AOS和客户端,安装后的编译和配置.如果一直跟随Reinhard的脚步,到这里,已经拥有一个没有数据的系统.     本节,Reinhard将要讲解怎样导入微软提供的测试数据.     首先,将AOS服务停止运行.进入计算机管理,服务,选中AOS服务,右键点击停止.稍等片刻,AOS服务已经停止运行.     接着,Reinhard要将当前空数据库进行备份,避免因导入测试数据失败造成的损失.     进入数据库管理

Changes in Microsoft Dynamics AX 2012 InventTrans data model

The purpose of this post is to give an overview about the changes been made in the Dynamics AX 2012 data model related to inventory transactions. Before AX2012, the only table used for recording all the inventory transactions was InventTrans.  In AX2

Microsoft Dynamics AX 2012: How to get Company,Customer and Vendor address in AX 2012

Scenario:  “How to get Addresses of “Customer, Vendor and Company” 1)      First we need to identify which table store address of each entity Table : LogisticsPostalAddress  : In Dynamics AX 2012 this is the main table which stores every address of e

[eBook]Inside Microsoft Dynamics AX 2012 R3发布

最近一本关于Microsoft Dynamics AX 2012开发的书<Inside Microsoft Dynamics AX 2012 R3> 发布. Book Description Fully updated for Microsoft Dynamics AX 2012 R3! Dig into the architecture and internals of Microsoft Dynamics AX 2012 R3 – with firsthand insights from

Dynamics AX 2012 R2 安装额外的AOS

    众所周知,AX系统分为三层:Client,Application Server,Database Server.     我们添加额外的Application Server主要是出于以下两个原因: 使用多台服务器,分担不同的角色(如批处理任务,报表,服务). 增加基础架构的弹性.     AX中的集群服务器,并不依托于Windows服务器,而是通过自己的技术实现的.它可以提高性能,但没有提高可用性.当一台服务器挂了,客户端会失去连接,任何正在处理的任务都会被回滚.重启客户端后,会连接到集

Dynamics AX 2012 - Report labels does not show up in report

Sometimes the report labels do not show in reports or the report shows label IDs like [email protected] instead of label values. There are two reasons to this type of issues: You have moved your AX service account from AX domain account from AX domai

Dynamics AX 2012 R3 Demo 安装与配置 - 安装数据服务器、AOS和客户端 (Step 2)

上一节中,Reinhard主要讲解了怎么配置安装环境,尤其是域控制器,并在域中添加了一个管理员账户 MSDynAX.NET\Reinhard ,以后的安装配置,均在该账户下进行. 现在运行 AX 2012 R3 的安装程序,选择安装组件. 选择安装AX,点击下一步. 选择自定义安装,点击下一步. 我们先安装数据库服务器,点击下一步. 安装程序会检查必要的组件是否安装完毕.安装完毕后,点击下一步. 选择船舰数据库,点击下一步. 因为我们将数据库服务器安装到本机,所以保持默认即可.如果要安装到其他机

安装Dynamics AX 2012 R2 AIF IIS上的Web服务

1.为什么使用IIS上的WEB服务 组件? 如果你要在Dynamics AX Service中使用HTTP Adapter,那么你就要安装IIS上的WEB服务 组件.HTTP Adapter会在IIS中生成一个Web Service. 2.安装IIS上的WEB服务 组件 下面讲讲怎么安装IIS上的WEB服务 组件.在服务器上,启动AX安装程序,选择添加或修改组件,选中IIS上的Web服务,下一步安装. 安装完毕后,会在在AX的系统管理>服务和应用集成框架>网站 中,添加了一个站点, 并在服务器