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/

  • 相关阅读:
    Execution Contexts (执行上下文)
    OOP—ECMAScript实现详解
    requireJS入门
    SqlServer 傲娇的表变量
    CSharp进阶 引用类型引发的血案
    CSharp进阶 都是请求惹的祸
    z-index问题
    js中事件(自定义事件)
    做了个后末日朋克风的梦
    昨晚的梦
  • 原文地址:https://www.cnblogs.com/bjdc/p/7252896.html
Copyright © 2011-2022 走看看