NaXeM
24-07-2004, 07:54 AM
Anyone know any good File Transfer Tutorials for delphi proeferbly n00b ones. I haven't done any sock programing with delphi or anything like tht.
TheLion
24-07-2004, 03:15 PM
You want to use a specific component set or just plain WinSock ?
NaXeM
25-07-2004, 07:43 AM
well ive done winsock in vb so yeah just winsock will do.
this is my code.
uses WinInet, ComCtrls, Forms, Windows, SysUtils;
downloading
function DownloadFTP(strHost, strUsername, strPassword, strRemoteDir, strRemoteFile, strLocalFile: String; iPort: Integer;
ProgressBar: TProgressBar; dwTransferMode: DWord = FTP_TRANSFER_TYPE_BINARY): 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, dwTransferMode, 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;
uploading
function UploadFTP(strHost, strUsername, strPassword, strRemoteDir, strRemoteFile, strLocalFile: String; iPort: Integer;
ProgressBar: TProgressBar; dwTransferMode: DWord = FTP_TRANSFER_TYPE_BINARY): Integer;
var
fLocalFile: File;
hNet: hInternet;
hFTP: hInternet;
hRemoteFile: hInternet;
iFileSize: DWord;
iBuffSize: DWord;
iBytesWritten: DWord;
iBuffWriteSize: DWord;
iBytesLeft: 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, dwTransferMode, 0);
if Assigned(hRemoteFile) then
Begin
AssignFile(fLocalFile, strLocalFile);
Reset(fLocalFile, 1);
iFileSize := FileSize(fLocalFile);
ProgressBar.Max := iFileSize;
iBuffSize := iBuffWriteSize;
iBytesLeft := iFileSize;
iBytesWritten := 0;
while iBytesLeft > 0 do
Begin
iBytesLeft := iFileSize - iBytesWritten;
if iBytesLeft < iBuffWriteSize then
Begin
SetLength(arrBuffer, iBytesLeft);
iBuffWriteSize := iBytesLeft;
end;
if iBytesLeft > 0 then
Begin
BlockRead(fLocalFile, arrBuffer[0], iBuffWriteSize);
if not InternetWriteFile(hRemoteFile, Pointer(arrBuffer), iBuffWriteSize, iBuffSize) then
Begin
Result := -7; // error writing file
Break;
end
else
end
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 upload
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;
that waht ure after? :)
NaXeM
25-07-2004, 11:58 PM
well tht's errr........a bit much lol im just looking for tutorials explaining how it's down in delphi i know ftp is faster but i like my winsock :P
:lol: basicly if u ripp that apart and take out all the error handling etc then u get this...
open a internet connection
connect to the server
set the current directory
open the remote file
create the local file
read the remote file
write the local file
loop last 2 until end :)
NaXeM
27-07-2004, 02:06 AM
Do u have anything for Winsock controls? this is for client file transfer jus messin around u know from one person to another not uploading or downloading files from a server.
the indy components have a demo on doing that. but i dont have any raw codes. mabey something for me to do in future :)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.