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 :)
Question
_tux_
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
this is the important bit
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
https://www.neowin.net/forum/topic/182541-delphi-ftp-corruption/Share on other sites
7 answers to this question
Recommended Posts