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/

  • 相关阅读:
    VS2013中使用码云gitee建立源代码管理
    win10激活出现错误0xc004f074 解决方案
    List<string> 去重复 并且出现次数最多的排前面
    jQuery.extend(),jQuery.fn.extend() 区别
    js 获取范围内的随机数
    xslt/xpath对不存在属性的判断问题
    查询各科成绩不合格与合格人数
    微信开发之实现一键拨号及短信发送功能
    Git 常用命令
    Excel 表 导入导出
  • 原文地址:https://www.cnblogs.com/bjdc/p/7252896.html
Copyright © 2011-2022 走看看