FTP from Axapta
from Development Axapta by (Dahlsgaard Jan)
add at the class declaration of the WinInet class the following:
DLLFunction internetConnect; DLLFunction ftpGetFile; DLLFunction ftpPutFile; DLLFunction setCurrentDirectory; #localmacro.FTP_TRANSFER_TYPE_BINARY 2 #endmacro
and this in the 'new':
internetConnect = new DLLFunction(_winInet,"InternetConnectA"); internetConnect.returns(ExtTypes::DWORD); internetConnect.arg(ExtTypes::DWORD); internetConnect.arg(ExtTypes::STRING); internetConnect.arg(ExtTypes::DWORD); internetConnect.arg(ExtTypes::STRING); internetConnect.arg(ExtTypes::STRING); internetConnect.arg(ExtTypes::DWORD); internetConnect.arg(ExtTypes::DWORD); internetConnect.arg(ExtTypes::DWORD); ftpGetFile = new DLLFunction(_winInet,"FtpGetFileA"); ftpGetFile.returns(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::STRING); ftpGetFile.arg(ExtTypes::STRING); ftpGetFile.arg(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::DWORD); ftpPutFile = new DLLFunction(_winInet,"FtpPutFileA"); ftpPutFile.returns(ExtTypes::DWORD); ftpPutFile.arg(ExtTypes::DWORD); ftpPutFile.arg(ExtTypes::STRING); ftpPutFile.arg(ExtTypes::STRING); ftpPutFile.arg(ExtTypes::DWORD); ftpPutFile.arg(ExtTypes::DWORD); setCurrentDirectory = new DLLFunction(winInetDLL, 'FtpSetCurrentDirectoryA'); setCurrentDirectory.returns(ExtTypes::DWord); setCurrentDirectory.arg(ExtTypes::DWord,ExtTypes::String);
and the following methods:
int internetConnect(str 60 _server, str 99 _userName, str 99 _password ) { return internetConnect.call(_handle,_server,0,_userName,_password,1,0,0); } int FtpGetFile(int _hdl, str 255 _remoteFile, str 255 _localFile) { return ftpGetFile.call(_hdl,_remoteFile,_localFile,false,0,#FTP_TRANSFER_TYPE_BINARY,0); } int FtpPutFile(int _hdl, str 255 _localFile, str 255 _remoteFile) { return ftpPutFile.call(_hdl,_localFile,_remoteFile,#FTP_TRANSFER_TYPE_BINARY,0); } boolean FtpSetCurrentDirectory(int _hdl, str _name) { return setCurrentDirectory.call(_hdl, _name) != 0; }
use the internetconnect method to connect ftp server, ftpgetfile, ftpputfile to get and put files, and ftpSetCurrentDirectory for change current diectory
Contents[hide] |
[edit]Upload File
static void TestUploadFTP(Args _args) { int handle; WinInet inet = new WinInet(); ; handle = inet.internetConnect("ftp.company.com","user123","password123"); inet.FtpSetCurrentDirectory(handle,"docs"); inet.FtpPutFile(handle,"C:/Dokumente und Einstellungen/User123/Eigene Dateien/test.txt","test.txt"); inet.internetCloseHandle(handle); }
[edit]Download File
static void TestUploadFTP(Args _args) { int handle; WinInet inet = new WinInet(); ; handle = inet.internetConnect("ftp.microsoft.com","",""); inet.FtpSetCurrentDirectory(handle,"MISC"); inet.FtpGetFile(handle,"CBCP.TXT","C:/Users/markus/CBCP.TXT"); inet.internetCloseHandle(handle); }
[edit]Troubleshooting
Problem: Connection can be established and directory change works. Download from FTP Server doesnt work, Dynamics AX ist not responding for a while. Afterwards FtpGetFile was not successful.
Solution: Create an exception for program Ax32.exe in your Windows Firewall
[edit]References
development-axapta : Re: How to send file to FTP FTP Sessions(Windows) MSDN
Since version 4 and in version 2009 you may use the .NET framework inside Dynamics AX.