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

 

转载于:https://www.cnblogs.com/bjdc/p/7252896.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值