Axapta 可以方便的通过COM对象访问FTP服务,代码示例如下:
Axapta中使用FTP
final class WinInet
{
DLL _winInet;
DLLFunction _internetOpen;
DLLFunction _internetCloseHandle;
DLLFunction _internetOpenUrl;
DLLFunction _internetReadFile;
int _handle;
int _result;
DLLFunction _internetConnect;
DLLFunction _ftpGetFile;
DLLFunction _ftpPutFile;
DLLFunction _ftpSetCurrentDirectory;
DLLFunction _ftpCommand;
DLLFunction _ftpFindFirstFile;
DLLFunction _ftpDeleteFile;
DLLFunction _ftpRenameFile;
DLLFunction _ftpCreateDirectory;
DLLFunction _ftpRemoveDirectory;
DLLFunction _ftpGetCurrentDirectory;
#define.FTP_TRANSFER_TYPE_BINARY(2)
#define.FTP_TRANSFER_TYPE_UNKNOWN(0)
#define.FTP_TRANSFER_TYPE_ASCII(1)
}
void finalize()
{
this.internetCloseHandle(_handle);
}
int ftpCommand(int _handler, boolean _expectResponseIn, str 255 _command)
{
int _handlerOut;
int _expectResponse;
;
if (_expectResponseIn)
_expectResponse = 1;
else
_expectResponse = 0;
if(_ftpCommand.call(_handler, _expectResponse, #FTP_TRANSFER_TYPE_ASCII, _command, 0, _handlerOut) == 0)
return 0;
else
return _handlerOut;
}
boolean ftpCreateDirectory(int _hdl, str 255 _name)
{
if (_ftpCreateDirectory.call(_hdl, _name) == 0)
return false;
else
return true;
}
boolean ftpDeleteFile(int _hdl, str 255 _file)
{
if (_ftpDeleteFile.call(_hdl, _file) == 0)
return false;
else
return true;
}
int ftpGetCurrentDirectory(int _hdl, str 255 _remoteFile, str 255 _localFile)
{
return 0;//_ftpGetCurrentDirectory.call(_hdl,_remoteFile,_localFile,false,0,#FTP_TRANSFER_TYPE_BINARY,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 ftpRemoveDirectory(int _hdl, str 255 _name)
{
if (_ftpRemoveDirectory.call(_hdl, _name) == 0)
return false;
else
return true;
}
boolean ftpRenameFile(int _hdl, str 255 _fileOld, str 255 _fileNew)
{
if (_ftpRenameFile.call(_hdl, _fileOld, _fileNew) == 0)
return false;
else
return true;
}
Boolean ftpSetCurrentDirectory(int _handler, str _name)
{
;
if (_handler == 0)
return false;
if (_ftpSetCurrentDirectory.call(_handler, _name) == 0)
return false;
else
return true;
}
boolean internetCloseHandle(int handle)
{
return _internetCloseHandle.call(handle);
}
int internetConnect(str 60 _server, str 99 _userName, str 99 _password )
{
return _internetConnect.call(_handle,_server,0,_userName,_password,1,0,0);
}
int internetOpen(str 255 agent,int accessType,str 255 proxyName,str 255 proxyByPass,int flags)
{
return _internetOpen.call(agent,accessType,proxyName,proxyByPass,flags);
}
int internetOpenUrl(str 500 url,int flags = 0,int context = 0)
{
Binary _headers = new Binary(512);
int hdl = _internetOpenUrl.call(_handle,url,"",-1,flags,context);
return hdl;
}
str internetReadFile(int hdl)
{
Binary _buffer = new Binary(2048);
Binary _bytesRead = new Binary(4);
str result;
boolean ret;
int done = 0;
while (! done)
{
ret = _internetReadFile.call(hdl,_buffer,2000,_bytesRead);
if ((! _bytesRead.dWord(0)) || (! ret))
done = 1;
else
{
result += subStr(_buffer.string(0),1,_bytesRead.dWord(0));
}
}
return ret ? result : "";
}
#winapi
int internetSaveFile(int hdl, str _fileName)
{
Binary _buffer = new Binary(2048);
Binary _bytesRead = new Binary(4);
int cnt = 0;
boolean ret;
DLLFunction fileCreate;
DLLFunction writeFile;
DLL dll = new Dll(@"KERNEL32.DLL");
int hFile;
;
if (_fileName)
{
fileCreate = new DLLFunction(dll, @"CreateFileA");
fileCreate.returns(ExtTypes::DWord);
fileCreate.arg(ExtTypes::String,
ExtTypes::DWord,
ExtTypes::DWord,
ExtTypes::DWord,
ExtTypes::DWord,
ExtTypes::DWord,
ExtTypes::DWord);
writeFile = new DllFunction(dll, @"WriteFile");
writeFile.arg(ExtTypes::DWord, ExtTypes::Pointer, ExtTypes::DWord, ExtTypes::Pointer, ExtTypes::DWord);
writeFile.returns(ExtTypes::DWord);
hFile = fileCreate.call(_fileName, #GENERIC_WRITE, 0, 0, #CREATE_ALWAYS, 0, 0);
while (hFile != #INVALID_HANDLE_VALUE)
{
ret = _internetReadFile.call(hdl,_buffer,2000,_bytesRead);
if (ret && _bytesRead.dWord(0))
{
cnt += _bytesRead.dWord(0);
writeFile.call(hFile, _buffer, _bytesRead.dWord(0), _bytesRead, 0);
}
else
break;
}
if (hFile != #INVALID_HANDLE_VALUE)
winapi::closeHandle(hFile);
}
return cnt;
}
void new()
{
try
{
_winInet = new DLL("WinInet");
}
catch(Exception::Internal)
{
infolog.clear();
return;
}
_internetOpen = new DLLFunction(_winInet,"InternetOpenA");
_internetOpen.returns(ExtTypes::DWORD);
_internetOpen.arg(ExtTypes::STRING);
_internetOpen.arg(ExtTypes::DWORD);
_internetOpen.arg(ExtTypes::STRING);
_internetOpen.arg(ExtTypes::STRING);
_internetOpen.arg(ExtTypes::DWORD);
_internetCloseHandle = new DLLFunction(_winInet,"InternetCloseHandle");
_internetCloseHandle.returns(ExtTypes::DWORD);
_internetCloseHandle.arg(ExtTypes::DWORD);
_internetOpenUrl = new DLLFunction(_winInet,"InternetOpenUrlA");
_internetOpenUrl.returns(ExtTypes::DWORD);
_internetOpenUrl.arg(ExtTypes::DWORD);
_internetOpenUrl.arg(ExtTypes::STRING);
_internetOpenUrl.arg(ExtTypes::STRING);
_internetOpenUrl.arg(ExtTypes::DWORD);
_internetOpenUrl.arg(ExtTypes::DWORD);
_internetOpenUrl.arg(ExtTypes::DWORD);
_internetReadFile = new DLLFunction(_winInet,"InternetReadFile");
_internetReadFile.returns(ExtTypes::DWORD);
_internetReadFile.arg(ExtTypes::DWORD);
_internetReadFile.arg(ExtTypes::POINTER);
_internetReadFile.arg(ExtTypes::DWORD);
_internetReadFile.arg(ExtTypes::POINTER);
_handle = this.internetOpen("Axapta",0,"","",0);
_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);
_ftpSetCurrentDirectory = new DLLFunction(_winInet, 'FtpSetCurrentDirectoryA');
_ftpSetCurrentDirectory.returns(ExtTypes::DWORD);
_ftpSetCurrentDirectory.arg(ExtTypes::DWORD);
_ftpSetCurrentDirectory.arg(ExtTypes::STRING);
_ftpCommand = new DLLFunction(_winInet, 'FtpCommandA');
_ftpCommand.returns(ExtTypes::DWORD);
_ftpCommand.arg(ExtTypes::DWORD);
_ftpCommand.arg(ExtTypes::DWORD);
_ftpCommand.arg(ExtTypes::DWORD);
_ftpCommand.arg(ExtTypes::STRING);
_ftpCommand.arg(ExtTypes::DWORD);
_ftpCommand.arg(ExtTypes::DWORD);
_ftpDeleteFile = new DLLFunction(_winInet, 'FtpDeleteFileA');
_ftpDeleteFile.returns(ExtTypes::DWORD);
_ftpDeleteFile.arg(ExtTypes::DWORD);
_ftpDeleteFile.arg(ExtTypes::STRING);
_ftpRenameFile = new DLLFunction(_winInet, 'FtpRenameFileA');
_ftpRenameFile.returns(ExtTypes::DWORD);
_ftpRenameFile.arg(ExtTypes::DWORD);
_ftpRenameFile.arg(ExtTypes::STRING);
_ftpRenameFile.arg(ExtTypes::STRING);
_ftpCreateDirectory = new DLLFunction(_winInet, 'FtpCreateDirectoryA');
_ftpCreateDirectory.returns(ExtTypes::DWORD);
_ftpCreateDirectory.arg(ExtTypes::DWORD);
_ftpCreateDirectory.arg(ExtTypes::STRING);
_ftpRemoveDirectory = new DLLFunction(_winInet, 'FtpRemoveDirectoryA');
_ftpRemoveDirectory.returns(ExtTypes::DWORD);
_ftpRemoveDirectory.arg(ExtTypes::DWORD);
_ftpRemoveDirectory.arg(ExtTypes::STRING);
_ftpGetCurrentDirectory = new DLLFunction(_winInet, 'FtpGetCurrentDirectoryA');
_ftpGetCurrentDirectory.returns(ExtTypes::DWORD);
_ftpGetCurrentDirectory.arg(ExtTypes::DWORD);
_ftpGetCurrentDirectory.arg(ExtTypes::STRING);
_ftpGetCurrentDirectory.arg(ExtTypes::STRING);
/*_ftpFindFirstFile = new DLLFunction(_winInet, 'FtpFindFirstFileA');
_ftpFindFirstFile.returns(ExtTypes::DWORD);
_ftpFindFirstFile.arg(ExtTypes::DWORD);
_ftpFindFirstFile.arg(ExtTypes::STRING);*/
}
boolean ok()
{
if (_winInet)
return TRUE;
else
return FALSE;
}