0 votes
ago by (260 points)
edited ago by

I am using FileServer as an SFTP server. When a user uploads a file, the
CreateFile(DirectoryNode parent, FileNode child) method—implemented via ReadWriteFileSystemProvider, gets triggered.

Within the child object, I can access details such as the file path and filename. However, the length parameter is always 0, even when the actual file size is greater than 0 bytes.

On the other hand, once the upload is completed, the FileUploaded event is triggered, where FileTransferredEventArgs.BytesTransferred correctly provides the actual file size.

My question:
How can I obtain the actual file size before the file data transfer begins in FileServer (SFTP)?

1 Answer

0 votes
ago by (151k points)

Unfortunately, this is not really possible due to the nature of the SFTP protocol itself. An SFTP upload is similar to creating a file stream and writing to it, where the final file length is not known until all writes have been made and the file is closed.

Some client's might try issuing an equivalent of FileStream.SetLength to allocate space for the file ahead of the actual upload, but that is not guaranteed to represent the actual file length, so it's not a good replacement.

Note: An SFTP client can actually seek in the remote file stream during the operation, so BytesTransferred is not even guaranteed to contain the correct value.

ago by (260 points)
Understood. Thank you for detailed reply.
...