Code:
function DownloadFTP(strHost, strUsername, strPassword, strRemoteDir, strRemoteFile, strLocalFile: String; iPort: Integer;
ProgressBar: TProgressBar): Integer;
const
iBuffReadSize= 4096;
var
fLocalFile: File;
hNet: hInternet;
hFTP: hInternet;
hRemoteFile: hInternet;
sRec: TWin32FindData;
iFileSize, iBuffSize, iBytesRead: DWord;
arrBuffer: Array[0..iBuffReadSize -1] of Byte;
Begin
if not (strHost = '') or (strUsername = '') or (strRemoteDir = '') or (strRemoteFile = '') or (strLocalFile = '') then
Begin
hNet := InternetOpen(PChar(Application.Title), INTERNET_OPEN_TYPE_PRECONFIG, '', '', 0);
if Assigned(hNet) then
Begin
hFTP := InternetConnect(hNet, PChar(strHost), iPort, PChar(strUserName), PChar(strPassword), INTERNET_SERVICE_FTP, 0, 0);
if Assigned(hFTP) then
Begin
if FtpSetCurrentDirectory(hFTP, PChar(strRemoteDir)) then
Begin
if FtpFindFirstFile(hFTP, PChar(strRemoteFile), sRec, 0, 0) <> nil then // find the file size
Begin
iFileSize := sRec.nFileSizeLow;
ProgressBar.Max := iFileSize;
hRemoteFile := FTPOpenFile(hFTP, PChar(strRemoteFile), GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 0);
if Assigned(hRemoteFile) then
Begin
AssignFile(fLocalFile, strLocalFile);
ReWrite(fLocalFile, 1);
iBuffSize := iBuffReadSize;
iBytesRead := 0;
while iBuffSize > 0 do
Begin
if not InternetReadFile(hRemoteFile, @arrBuffer, iBuffReadSize, iBuffSize) then
Begin
Result := -7; // error reading file
Break;
end;
if (iBuffSize > 0) and (iBuffSize <= iBuffReadSize) then
BlockWrite(fLocalFile, arrBuffer[0], iBuffSize)
else
Result := 1; // reached end of file
iBytesRead := iBytesRead + iBuffSize;
ProgressBar.Position := iBytesRead;
Application.ProcessMessages;
end;
CloseFile(fLocalFile);
InternetCloseHandle(hRemoteFile);
end
else
Result := -6; // error opening remote file
end
else
Result := -5; // cannot find the file to download
end
else
Result := -4; // cannot change directory
InternetCloseHandle(hFTP);
end
else
Result := -3; // host unnavailible
InternetCloseHandle(hNet);
end
else
Result := -2; // unable to access WinInet.dll
end
else
Result := -1; // empty connection strings
end;
function UploadFTP(strHost, strUsername, strPassword, strRemoteDir, strRemoteFile, strLocalFile: String; iPort: Integer;
ProgressBar: TProgressBar): Integer;
var
fLocalFile: File;
hNet: hInternet;
hFTP: hInternet;
hRemoteFile: hInternet;
iFileSize: DWord;
iBuffSize: DWord;
iBytesWritten: DWord;
iBuffWriteSize: DWord;
arrBuffer: Array of Byte;
sRec: TWin32FindData;
Begin
iBuffWriteSize := 4096;
SetLength(arrBuffer, iBuffWriteSize -1);
if not (strHost = '') or (strUsername = '') or (strRemoteDir = '') or (strRemoteFile = '') or (strLocalFile = '') then
Begin
hNet := InternetOpen(PChar(Application.Title), INTERNET_OPEN_TYPE_PRECONFIG, '', '', 0);
if Assigned(hNet) then
Begin
hFTP := InternetConnect(hNet, PChar(strHost), iPort, PChar(strUserName), PChar(strPassword), INTERNET_SERVICE_FTP, 0, 0);
if Assigned(hFTP) then
Begin
if FtpSetCurrentDirectory(hFTP, PChar(strRemoteDir)) then
Begin
if FtpFindFirstFile(hFTP, PChar(strRemoteFile), sRec, 0, 0) <> nil then // see if the file exists
Begin
FtpDeleteFile(hFTP, PChar(strRemoteFile));
end;
if FileExists(strLocalFile) then
Begin
hRemoteFile := FtpOpenFile(hFTP, PChar(strRemoteFile), GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY, 0);
if Assigned(hRemoteFile) then
Begin
AssignFile(fLocalFile, strLocalFile);
Reset(fLocalFile, 1);
iFileSize := FileSize(fLocalFile);
ProgressBar.Max := iFileSize;
iBuffSize := iBuffWriteSize;
iBytesWritten := 0;
while iBuffSize > 0 do
Begin
if iFileSize - iBytesWritten < iBuffWriteSize then
Begin
SetLength(arrBuffer, iFileSize - iBytesWritten);
iBuffWriteSize := iFileSize - iBytesWritten;
end;
BlockRead(fLocalFile, arrBuffer[0], iBuffWriteSize);
if (iBuffSize > 0) and (iBuffSize <= iBuffWriteSize) then
if not InternetWriteFile(hRemoteFile, @arrBuffer, iBuffWriteSize, iBuffSize) then
Begin
Result := -7; // error writing file
Break;
end
else
else
Begin
Result := 1; // end of file
Break;
end;
iBytesWritten := iBytesWritten + iBuffSize;
ProgressBar.Position := iBytesWritten;
Application.ProcessMessages;
end;
CloseFile(fLocalFile);
InternetCloseHandle(hRemoteFile);
end
else
Result := -6; // error opening remote file
end
else
Result := -5; // cannot find the file to download
end
else
Result := -4; // cannot change directory
InternetCloseHandle(hFTP);
end
else
Result := -3; // host unnavailible
InternetCloseHandle(hNet);
end
else
Result := -2; // unable to access WinInet.dll
end
else
Result := -1; // empty connection strings
end;
its probably just something simple. anyone have any ideas why?
Bookmarks