Page 1 of 1

Overwriting file on upload

Posted: Fri 03 May 2019 08:10
by HBL
I have a program that sync. files between a local drive and the files on our SFTP server.

I noticed a bug (I belive it is). When I try to sync a file that is smaller then the file on the SFTP, it seems it upload the file, but in reality its not.

Uploading a smaller file from local HDD to SFTP where a larger file is present - Not Working
Uploading a larger file from local HDD to SFTP where a smaller file is present - Working
Uploading a file that dosnt exists on SFTP - Working.

My quick fix is to delete the SFTP file first (if exists)

I use the following code to upload a file:
Handle := FSFTPClient.OpenFile(RemotePath + RemoteFile, fmCreateNew, [], [bmWrite, bmDelete], [amWriteData]);
Try
BufSize := (High(Word)+1) * ((BUF_SIZE * 1024 * 1024) div High(Word));
Setlength(Buf, BufSize);

FileOffset := 0;
BufSize := 0;
Repeat
PartUploadDataProc(Buf, BufSize, EOF);
FSFTPClient.WriteFile(Handle, FileOffset, Buf, 0, BufSize);
Inc(FileOffset, BufSize);

Until
EOF = True;
Finally
FSFTPClient.CloseHandle(Handle);
end;

Re: Overwriting file on upload

Posted: Mon 06 May 2019 12:26
by ViktorV
To solve your task, you can try using the fmCreateOrTruncate parameter in the TScSFTPClient.OpenFile method.