PDA

View Full Version : File Transfer tuts.



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.

tux
25-07-2004, 03:57 PM
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&#40;hFTP, PChar&#40;strRemoteFile&#41;, sRec, 0, 0&#41; <> nil then // find the file size
Begin
iFileSize &#58;= sRec.nFileSizeLow;
ProgressBar.Max &#58;= iFileSize;

hRemoteFile &#58;= FTPOpenFile&#40;hFTP, PChar&#40;strRemoteFile&#41;, GENERIC_READ, dwTransferMode, 0&#41;;

if Assigned&#40;hRemoteFile&#41; then
Begin
AssignFile&#40;fLocalFile, strLocalFile&#41;;
ReWrite&#40;fLocalFile, 1&#41;;

iBuffSize &#58;= iBuffReadSize;
iBytesRead &#58;= 0;

while iBuffSize > 0 do
Begin
if not InternetReadFile&#40;hRemoteFile, @arrBuffer, iBuffReadSize, iBuffSize&#41; then
Begin
Result &#58;= -7; // error reading file
Break;
end;

if &#40;iBuffSize > 0&#41; and &#40;iBuffSize <= iBuffReadSize&#41; then
BlockWrite&#40;fLocalFile, arrBuffer&#91;0&#93;, iBuffSize&#41;
else
Result &#58;= 1; // reached end of file

iBytesRead &#58;= iBytesRead + iBuffSize;
ProgressBar.Position &#58;= iBytesRead;
Application.ProcessMessages;
end;

CloseFile&#40;fLocalFile&#41;;
InternetCloseHandle&#40;hRemoteFile&#41;;
end
else
Result &#58;= -6; // error opening remote file
end
else
Result &#58;= -5; // cannot find the file to download
end
else
Result &#58;= -4; // cannot change directory

InternetCloseHandle&#40;hFTP&#41;;
end
else
Result &#58;= -3; // host unnavailible

InternetCloseHandle&#40;hNet&#41;;
end
else
Result &#58;= -2; // unable to access WinInet.dll
end
else
Result &#58;= -1; // empty connection strings
end;

uploading

function UploadFTP&#40;strHost, strUsername, strPassword, strRemoteDir, strRemoteFile, strLocalFile&#58; String; iPort&#58; Integer;
ProgressBar&#58; TProgressBar; dwTransferMode&#58; DWord = FTP_TRANSFER_TYPE_BINARY&#41;&#58; Integer;
var
fLocalFile&#58; File;
hNet&#58; hInternet;
hFTP&#58; hInternet;
hRemoteFile&#58; hInternet;
iFileSize&#58; DWord;
iBuffSize&#58; DWord;
iBytesWritten&#58; DWord;
iBuffWriteSize&#58; DWord;
iBytesLeft&#58; DWord;
arrBuffer&#58; Array of Byte;
sRec&#58; TWin32FindData;

Begin
iBuffWriteSize &#58;= 4096;
SetLength&#40;arrBuffer, iBuffWriteSize -1&#41;;

if not &#40;&#40;strHost = ''&#41; or &#40;strUsername = ''&#41; or &#40;strRemoteDir = ''&#41; or &#40;strRemoteFile = ''&#41; or &#40;strLocalFile = ''&#41;&#41; then
Begin
hNet &#58;= InternetOpen&#40;PChar&#40;Application.Title&#41;, INTERNET_OPEN_TYPE_PRECONFIG, '', '', 0&#41;;

if Assigned&#40;hNet&#41; then
Begin
hFTP &#58;= InternetConnect&#40;hNet, PChar&#40;strHost&#41;, iPort, PChar&#40;strUserName&#41;, PChar&#40;strPassword&#41;, INTERNET_SERVICE_FTP, 0, 0&#41;;

if Assigned&#40;hFTP&#41; then
Begin
if FtpSetCurrentDirectory&#40;hFTP, PChar&#40;strRemoteDir&#41;&#41; then
Begin
if FtpFindFirstFile&#40;hFTP, PChar&#40;strRemoteFile&#41;, sRec, 0, 0&#41; <> nil then // see if the file exists
Begin
FtpDeleteFile&#40;hFTP, PChar&#40;strRemoteFile&#41;&#41;;
end;

if FileExists&#40;strLocalFile&#41; then
Begin
hRemoteFile &#58;= FtpOpenFile&#40;hFTP, PChar&#40;strRemoteFile&#41;, GENERIC_WRITE, dwTransferMode, 0&#41;;

if Assigned&#40;hRemoteFile&#41; then
Begin
AssignFile&#40;fLocalFile, strLocalFile&#41;;
Reset&#40;fLocalFile, 1&#41;;

iFileSize &#58;= FileSize&#40;fLocalFile&#41;;
ProgressBar.Max &#58;= iFileSize;

iBuffSize &#58;= iBuffWriteSize;
iBytesLeft &#58;= iFileSize;
iBytesWritten &#58;= 0;

while iBytesLeft > 0 do
Begin
iBytesLeft &#58;= iFileSize - iBytesWritten;
if iBytesLeft < iBuffWriteSize then
Begin
SetLength&#40;arrBuffer, iBytesLeft&#41;;
iBuffWriteSize &#58;= iBytesLeft;
end;

if iBytesLeft > 0 then
Begin
BlockRead&#40;fLocalFile, arrBuffer&#91;0&#93;, iBuffWriteSize&#41;;
if not InternetWriteFile&#40;hRemoteFile, Pointer&#40;arrBuffer&#41;, iBuffWriteSize, iBuffSize&#41; then
Begin
Result &#58;= -7; // error writing file
Break;
end
else
end
else
Begin
Result &#58;= 1; // end of file
Break;
end;

iBytesWritten &#58;= iBytesWritten + iBuffSize;

ProgressBar.Position &#58;= iBytesWritten;
Application.ProcessMessages;
end;

CloseFile&#40;fLocalFile&#41;;
InternetCloseHandle&#40;hRemoteFile&#41;;
end
else
Result &#58;= -6; // error opening remote file
end
else
Result &#58;= -5; // cannot find the file to upload
end
else
Result &#58;= -4; // cannot change directory

InternetCloseHandle&#40;hFTP&#41;;
end
else
Result &#58;= -3; // host unnavailible

InternetCloseHandle&#40;hNet&#41;;
end
else
Result &#58;= -2; // unable to access WinInet.dll
end
else
Result &#58;= -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

tux
26-07-2004, 02:01 PM
: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.

tux
27-07-2004, 01:10 PM
the indy components have a demo on doing that. but i dont have any raw codes. mabey something for me to do in future :)