Extended File Transfer Sample
Purpose
Some time ago we received several questions in our newsgroup asking how to download
big files using the RemObjects SDK. This sample presents a working solution.
How it works
The approach is quite simple,instead of downloading a complete file at once, it
is split into small chunks to be downloaded separately. On the client side, these are then combined back into a single file.
This sample provides a RemObject server that exposes the content of a folder (see the GetFilesList method in the ExtendedFileTransferService implementation)
and a client that can connect to the server to obtain a list of the files available for downloading. The server also provides another method for downloading a chunk
of a file called DownloadFilePart.
This method requires three parameters:
- FileName (AnsiString) - name of the file to be downloaded.
- PartNo (int32) - the chunk part that should be downloaded starting with 1.
- PartSize (int32) - size of of chunk that should be downloaded.
DownloadFilePart reads the section of the file with offset PartSize*PartNo and returns the data as a Binary stream to the client.
When we want to download a file we can specify size of chunk (Below the Chunk Size combobox you will see calculated total count of chunks).
Also we can specify the number of
threads we want to use for the downloading operation. Since each partial chunk download
is independent, using threads is quite attractive in this scenario.
Examine the code
- See the implementation of the GetFilesList and DownloadFilePart methods in the ExtendedFileTransferService_Impl class. Look how we can read a chunk of file and return it back to the client.
- See DownloadPart function on the client side. This is the method that executes inside each thread we run to download the selected file. Note that we use a separate channel and message for each thread.
- Review the bDownload_Click handler. Here we are starting the download. Look how we are using a thread pool for controlling the number of the threads that are
involved in the downloading process.
Getting started
- Compile and run the server and client applications.
- Put a file into shared folder (you can easily locate and open this folder when you
click on the link label on the server main form).
- Get the available files on the client side by clicking the GetFiles button.
- Adjust the chunk size and thread count on the client.
- Try to download the selected file. At the end of downloading process, you can open
this file to ensure that downloading was successful.