• 0

[delphi] ftp corruption


Question

ive written my own code to connect to an ftp server and download a file, that works perfectly. so i then wrote code to upload to a ftp, this is based on the download code but it doesnt work :pinch:

when the file gets uploaded it becomes corrupted. ive done a breakpoint on the InternetWrite and the databuffer going to the procedure is NOT corrupted, but the end result on the ftp server is :(

anyway, heres the code. i have put the important bit into its own code block

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;
 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, FTP_TRANSFER_TYPE_BINARY, 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;

this is the important bit

                                       if iBytesLeft > 0 then
                                          Begin
                                            BlockRead(fLocalFile, arrBuffer[0], iBuffWriteSize);
                                            if not InternetWriteFile(hRemoteFile, @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 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;

this is driving me crazy, because this is needed for a feature for my program :( any help at all is apreciated :)

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

i did look at ftpPutFile but i dont see a way to implement a progress bar. however for what i need it for in my project it doesnt matter. but for future it will be good with a progress bar :p

also, it corrupted a binary file as well so thats not a problem, and my ftp app is set to allways binary and never corrupts things :s

thanks for the help :)

[edit] looked on ms site and have a vague idea on how to do progress bar

Edited by _tux_
Link to comment
Share on other sites

  • 0

ok. i just changed my code to use the ftpPutFile function and tryed ASCII and BINARY and its still corrupt.

http://www.deviousdevelopment.com/skin2.ini (ascii mode)

                         if ftpPutFile(hFTP, PChar(strLocalFile), PChar(strRemoteFile), FTP_TRANSFER_TYPE_ASCII, 0) then
                            Result := 1 // file done
                          else
                            Result := -6; // error putting file

it returns 1

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.