Results 1 to 8 of 8

Thread: File Transfer tuts.

  1. #1

    File Transfer tuts.

    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.
    Weeeeeeee!!!!!!!

  2. #2

    File Transfer tuts.

    You want to use a specific component set or just plain WinSock ?
    Do it by the book, but be the author!
    <br />
    <br />Visit the Lion Productions website at:
    <br />http://lionprod.f2o.org

  3. #3

    File Transfer tuts.

    well ive done winsock in vb so yeah just winsock will do.
    Weeeeeeee!!!!!!!

  4. #4

    File Transfer tuts.

    this is my code.

    uses WinInet, ComCtrls, Forms, Windows, SysUtils;

    downloading
    Code:
    function DownloadFTP&#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;
    const
     iBuffReadSize= 4096;
    
    var
     fLocalFile&#58; File;
     hNet&#58; hInternet;
     hFTP&#58; hInternet;
     hRemoteFile&#58; hInternet;
     sRec&#58; TWin32FindData;
     iFileSize, iBuffSize, iBytesRead&#58; DWord;
     arrBuffer&#58; Array&#91;0..iBuffReadSize -1&#93; of Byte;
    
    Begin
      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 // 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
    Code:
    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?

  5. #5

    File Transfer tuts.

    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
    Weeeeeeee!!!!!!!

  6. #6

    File Transfer tuts.

    :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

  7. #7

    File Transfer tuts.

    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.
    Weeeeeeee!!!!!!!

  8. #8

    File Transfer tuts.

    the indy components have a demo on doing that. but i dont have any raw codes. mabey something for me to do in future

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •