zoukankan      html  css  js  c++  java
  • 【转】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/

  • 相关阅读:
    立即执行函数
    刷题-函数-闭包-返回函数
    刷题-js对象-属性遍历
    并发——无缓冲通道,带缓冲的通道,通道的多路复用,关闭通道
    并发——轻量级线程,通道,单向通道
    包——基本概念,自定义包,创建包,导出包中的标志符
    接口——嵌套,接口和类型间的转换,空接口类型,类型分支
    接口——定义,实现接口的条件,类型与接口的关系,类型断言
    结构体——内嵌,初始化内嵌结构体,内嵌结构体成员名字冲突
    结构体——方法和接收器,为任意类型添加方法
  • 原文地址:https://www.cnblogs.com/bjdc/p/7252896.html
Copyright © 2011-2022 走看看